Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
How to set up Next.js render in Express routing?

app.js

const express = require('express');
const next = require('next');
const port = parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== 'production';

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

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

    const indexRouter = require('./routes/indexRouter');

    server.use(indexRouter);
    server.all('*', (req, res) => {
      return handle(req, res);
    });

    server.listen(port, (err) => {
      if (err) throw err;
      console.log('> Ready on http://localhost:' + port);
    });
  })
  .catch((ex) => {
    console.error(ex.stack);
    process.exit(1);
  });

indexRoutes.js

const express = require('express');
const router = express.Router();
const userController = require('../controllers/user');

router.post('/api/signup', userController.signup);

I want to render the component via Next.js in indexRoutes.js. I've seen others do in the main app.js.

server.get('/login', (req, res) => {
  app.render(req, res, '/login');
});

But I don't understand how I can do it in a separate file. I tried to do this, but no result.

indexRoutes.js

const express = require('express');
const next = require('next');
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const router = express.Router();
const userController = require('../controllers/user');

router.post('/api/signup', userController.signup);
router.get('/login', (req, res) => {
  return app.render(req, res, '/login'); // this does not work
});

How can I solve my problem?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

If you want to put the routes in a separate file you can create the following indexRouter.js file:

// routes/indexRouter.js

const userController = require('../controllers/user');

const indexRouter = (server, app) => {
    server.post('/api/signup', userController.signup);
    server.get('/login', (req, res) => {
        return app.render(req, res, '/login');
    });
};

module.exports = indexRouter;

You can then require it from app.js and pass the server and app objects to it.

// app.js

// ...

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

        const indexRouter = require('./routes/indexRouter');

        indexRouter(server, app)
        
        server.all('*', (req, res) => {
            return handle(req, res);
        });

        server.listen(port, (err) => {
            if (err) throw err;
            console.log('> Ready on http://localhost:' + port);
        });
    })
    .catch((ex) => {
        console.error(ex.stack);
        process.exit(1);
    });
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!