0
0
NextJSframework~10 mins

Self-hosting with Node.js in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Self-hosting with Node.js
Write Next.js app
Build app with next build
Start Node.js server with next start
Server listens on port
Browser sends request
Node.js server handles request
Render page and send response
Browser displays page
This flow shows how you build and run a Next.js app using Node.js to serve pages to browsers.
Execution Sample
NextJS
import next from 'next';
import http from 'http';

const app = next({ dev: false });
const handle = app.getRequestHandler();

app.prepare().then(() => {
  http.createServer((req, res) => handle(req, res)).listen(3000);
});
This code creates a Node.js server that runs a built Next.js app and listens on port 3000.
Execution Table
StepActionState BeforeState AfterOutput/Effect
1Import next and http modulesNo app or serverModules loadedReady to create app and server
2Create Next.js app instance with dev:falseNo app instanceApp instance createdApp ready for production mode
3Get request handler from appNo handlerHandler function readyCan handle HTTP requests
4Call app.prepare()App not preparedApp preparedBuild assets ready to serve
5Create HTTP server with handlerNo serverServer createdServer ready to listen
6Server listens on port 3000Server createdServer listeningWaiting for browser requests
7Browser sends HTTP requestServer listeningRequest receivedServer processes request
8Handler processes requestRequest receivedResponse generatedPage HTML ready
9Server sends responseResponse generatedResponse sentBrowser receives page
10Browser renders pageResponse receivedPage displayedUser sees website
11No more requestsServer listeningIdleWaiting for next request
💡 Server runs continuously until stopped; no exit unless manually terminated.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5After Step 6After Step 9
appundefinedNext.js app instancePrepared appPrepared appPrepared appPrepared app
handleundefinedHandler functionHandler functionHandler functionHandler functionHandler function
serverundefinedundefinedundefinedHTTP server createdServer listeningServer listening
Key Moments - 3 Insights
Why do we call app.prepare() before starting the server?
app.prepare() builds and prepares the Next.js app assets so the server can serve pages correctly. See execution_table step 4.
What does the handler function do when the server receives a request?
The handler processes the HTTP request and generates the correct page response. See execution_table steps 7 and 8.
Does the server stop after sending one page?
No, the server keeps listening for more requests until manually stopped. See execution_table step 11.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of the server after step 6?
AServer listening on port 3000
BServer not created yet
CServer stopped
DServer preparing app
💡 Hint
Check the 'State After' column for step 6 in execution_table.
At which step does the Next.js app finish preparing and is ready to serve pages?
AStep 7
BStep 2
CStep 4
DStep 9
💡 Hint
Look for app.prepare() completion in execution_table.
If we skip calling app.prepare(), what will happen when the server tries to handle requests?
AServer will still serve pages normally
BPages may not load correctly because assets are not ready
CServer will crash immediately
DServer will listen on a different port
💡 Hint
Consider the role of app.prepare() in execution_table step 4.
Concept Snapshot
Self-hosting Next.js with Node.js:
1. Build your Next.js app with 'next build'.
2. Create a Node.js server using 'next' and 'http' modules.
3. Call app.prepare() to ready assets.
4. Start server with http.createServer and listen on a port.
5. Server handles browser requests and serves pages.
6. Server runs continuously until stopped.
Full Transcript
This visual execution trace shows how to self-host a Next.js app using Node.js. First, you write your Next.js app code. Then you build it using 'next build'. Next, you create a Node.js server by importing 'next' and 'http' modules. You create a Next.js app instance with production mode off, then get the request handler. You call app.prepare() to prepare the app assets. After that, you create an HTTP server that uses the handler to process requests. The server listens on port 3000. When a browser sends a request, the server receives it, the handler processes it, and the server sends back the rendered page. The browser then displays the page. The server keeps running and waiting for more requests until you stop it manually. Key points include calling app.prepare() before starting the server to ensure assets are ready, and understanding that the handler function processes each incoming request. This setup allows you to serve your Next.js app yourself using Node.js.