// server.jsconstexpress=require('express');constnextJS=require('next');asyncfunctionstart(){constdev=process.env.NODE_ENV!=='production';constapp=nextJS({dev});constserver=express();awaitapp.prepare();// Redirect all requests to main entrypoint pages/index.jsserver.get('/*',async(req,res,next)=>{try{app.render(req,res,'/');}catch(e){next(e);}});server.listen(3000,err=>{if(err)throwerr;console.log(`> Ready on http://localhost:3000`);});}start();

Run the dev server, and the entrypoint page at pages/index.js should be served as response for any requested url. 👊

Run the dev server, and you should be able to see your routes live and server side rendered.

Part two, context information

One of my favourite react-router features consists of the possibility of adding context information during the rendering phase and returning server side responses based on the information collected into the context object.

This enables client side code to take control of the responses returned by the node server like returning a HTTP 404 instead of a "not found page" or returning a real HTTP 302 redirect instead of a client side one.

In order to achieve this behaviour we have to configure Next.js to do the following:

render the requested page providing a context object to the app router

check whether context object was mutated during the rendering process

decide whether to return the rendered page or do something else based on context object

Hello Andrea,
I tried to do the same thing but just felt I shouldn't use next.js if I don't let it handle the routing and SSR things. How do you think about it? Could you still feel any benefits of using next.js after doing this?