Next.js Discord

Discord Forum

next.js express

Answered
Southeastern blueberry bee posted this in #help-forum
Open in Discord
Southeastern blueberry beeOP
const express = require("express");
const next = require("next");

const port = 3000;
const dev = "production";
const app = next({ dev });
const handle = app.getRequestHandler();

app.prepare().then(() => {
  const server = express();

  server.get("/", (req, res) => {
    return app.render(req, res, "/");
  });
  
  server.get("*", (req, res) => {
    return handle(req, res);
  });

  server.listen(port, (err) => {
    if (err) throw err;
    console.log(`Ready on http://localhost:${port}`);
  });
});


how do i get the nextjs website from a custom dir not in the same dir as the index.js

Error: > Couldn't find any `pages` or `app` directory. Please create one under the project root
Answered by Ray
import { createServer } from "http";
import next from "next";
import { parse } from "url";

const dev = process.env.NODE_ENV !== "production";
const port = parseInt(process.env.PORT || '3000', 10)
const app = next({ dev, dir: "../client" });

const handle = app.getRequestHandler();

app.prepare().then(() => {
  createServer((req, res) => {
    const parsedUrl = parse(req.url!, true);
    handle(req, res, parsedUrl);
  }).listen(port);

  console.log(
    `> Server listening at http://localhost:${port} as ${
      dev ? "development" : process.env.NODE_ENV
    }`
  );
});
View full answer

30 Replies

Southeastern blueberry beeOP
my dir

/ client
  / the website page.tsx and everything
/ server
  / node_modules
  / index.js ( contains code at the top )
  / package.json
  / etc
@Southeastern blueberry bee my dir / client / the website page.tsx and everything / server / node_modules / index.js ( contains code at the top ) / package.json / etc
import { createServer } from "http";
import next from "next";
import { parse } from "url";

const dev = process.env.NODE_ENV !== "production";
const port = parseInt(process.env.PORT || '3000', 10)
const app = next({ dev, dir: "../client" });

const handle = app.getRequestHandler();

app.prepare().then(() => {
  createServer((req, res) => {
    const parsedUrl = parse(req.url!, true);
    handle(req, res, parsedUrl);
  }).listen(port);

  console.log(
    `> Server listening at http://localhost:${port} as ${
      dev ? "development" : process.env.NODE_ENV
    }`
  );
});
Answer
/ client
/ app or pages
/ page.jsx + layout.jsx or index.jsx
understand what?
Southeastern blueberry beeOP
how much you helped
😆
Southeastern blueberry beeOP
❤️
use typescript and it will tell you
Southeastern blueberry beeOP
im using js in index.js
and ts for the website
or client
ah it can be typescript too
Southeastern blueberry beeOP
true
oh I get the typedef with js too
nm please mark solutions:blob_yes:
Southeastern blueberry beeOP
where
oh
right click the answer
you got it👍🏾
Southeastern blueberry beeOP
tysm
np
Original message was deleted
MonoLisa
its fine i think
express?
@Ray Click to see attachment
Southeastern blueberry beeOP
how do i send a dynamic route from express to the next i have
server.get('/:username', async (req, res) => {
    return app.render(req, res, '/user')
})


but how do i get the username from the next.js
@Southeastern blueberry bee how do i send a dynamic route from express to the next i have js server.get('/:username', async (req, res) => { return app.render(req, res, '/user') }) but how do i get the username from the next.js
server.get('/:username', async (req, res) => {
    return app.render(req, res, `/user/${req.params.username}`)
})

// app/user/[username]/page.tsx
export default function Page({ params }: { params: { username: string } ) {
  return <div>{params.username}</div>
}