如何为基础网页定义home路由

如何为基础网页定义home路由

本文介绍了使用Next.js next-routes,如何为基础网页定义home路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用 next-routes 并关闭默认/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');

似乎不是来自 locahost:3000/

does not seem to may from locahost:3000/

我在 repo 中看到一个我认为类似但没有解决方案的问题.

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

推荐答案

我认为不是 useFileSystemPublicRoutes: true 你只需要创建一个自定义的 server.js

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);
});

然后有一个 routes.js

and then have a routes.js

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

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

运行 node server.js 并测试 / 路径.

Run node server.js and test / path.

这篇关于使用Next.js next-routes,如何为基础网页定义home路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 08:39