new·Earn with mozg — 20% of every monthSend somebody here and take a fifth of every plan payment they make, for as long as they keep paying — not a bounty on the first invoice. Your handle is the link, the window is thirty days, and the commission lands on your balance the second they pay. Free to join: if you have signed in, you already have the link. mozg.sh/earnall news →
mozg.beta
Sign in

Next.js · API reference · all subjects

custom server

8 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Custom server incompatibility with standalone output mode

When using standalone output mode, it does not trace custom server files. Standalone mode outputs a separate minimal server.js file instead. Custom servers and standalone output mode cannot be used together.

server.js syntax requirements

The server.js file does not run through the Next.js Compiler or bundling process. The syntax and source code must be compatible with the current Node.js version being used.

app.prepare() must complete before listening

The app.prepare() method returns a promise that must resolve before the server listens for requests. This is shown in the pattern: app.prepare().then(() => { createServer(...).listen(port); }).

Custom server creation with next import

The next import is a function that receives an object with configuration options. It returns an app object that can be used to handle requests. Basic usage: import next from 'next'; const app = next({}); const handle = app.getRequestHandler();

next() function options

The next() function accepts an options object with the following properties: conf (Object, defaults to {}), dev (Boolean, optional, defaults to false), dir (String, optional, defaults to '.'), quiet (Boolean, optional, defaults to false), hostname (String, optional), port (Number, optional), httpServer (node:http#Server, optional), turbopack (Boolean, optional, enabled by default), webpack (Boolean, optional).

Custom server example with Node.js http module

Example custom server implementation: import { createServer } from 'http'; import next from 'next'; const port = parseInt(process.env.PORT || '3000', 10); const dev = process.env.NODE_ENV !== 'production'; const app = next({ dev }); const handle = app.getRequestHandler(); app.prepare().then(() => { createServer((req, res) => { handle(req, res) }).listen(port); console.log(`> Server listening at http://localhost:${port} as ${dev ? 'development' : process.env.NODE_ENV}`); });

Custom server package.json scripts

For a custom server, update package.json scripts: dev should run 'node server.js', build should run 'next build', and start should run 'NODE_ENV=production node server.js'.

Custom server use cases and when to avoid

A custom server allows programmatic control over the Next.js server startup. However, it should only be used when the integrated Next.js router cannot meet application requirements. Most applications do not need a custom server.

Give your agent this brain