Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Next.js next-routes, how to define a home route for the base web page?

When using next-routes, and turning off default /page routing

useFileSystemPublicRoutes: true

how can you get the root page of a web site to display?

I can't seem to get any base route to work. For example, this route:

routes.add("homeRoute",'/','homePage');

does not seem to may from locahost:3000/

I see an issue here in the repo that I think is similar but with no solution.

https://github.com/fridays/next-routes/issues/228

like image 228
Peter Kellner Avatar asked Aug 31 '25 02:08

Peter Kellner


1 Answers

I think instead of useFileSystemPublicRoutes: true you just need to create a custom server.js

// server.js
const next = require('next');
const routes = require('./routes');
const app = next({ dev: process.env.NODE_ENV !== 'production' });
const handler = routes.getRequestHandler(app);

const { createServer } = require('http');
app.prepare().then(() => {
  createServer(handler).listen(3000);
});

and then have a routes.js

// routes.js
const routes = require('next-routes');

module.exports = routes().add({
  name: 'homeRoute',
  pattern: '/',
  page: 'homePage'
});

Run node server.js and test / path.

like image 136
iurii Avatar answered Sep 02 '25 15:09

iurii