javascript - 在此服务器上未找到请求的 URL /about-us
问题描述
执行导出后,在主机上,当我手动输入链接或刷新页面时,没有显示消息。我有 server.js 和下一个模块。我还需要做其他事情吗?
服务器.js
const express = require('express');
const next = require('next');
const { parse } = require('url');
const DEV = process.env.ENVIRONMENT !== 'production';
const PORT = 4567;
const app = next({dir: '.', dev: DEV});
const handle = app.getRequestHandler();
const getRoutes = require('./routes');
const routes = getRoutes();
app.prepare().then(() => {
const server = express();
server.get('*', (req, res) => {
const parsedUrl = parse(req.url, true);
const { pathname, query } = parsedUrl;
const route = routes[pathname];
if (route) {
return app.render(req, res, route.page, route.query);
}
return handle(req, res);
});
server.listen(PORT, (err) => {
if (err) throw err;
console.log(`> READY FOR LIFOTFF http://localhost:${PORT}`);
});
});
路由器.js
module.exports = () => {
return {
'/': { page: '/' },
'/404': { page: '/404' },
'/about-us': { page: '/about-us' },
}
}
next.config.js
const getRoutes = require('./routes');
module.exports = {
useFileSystemPublicRoutes: true,
exportPathMap: getRoutes
}
解决方案
我添加trailingSlash: true
到 next.config.js,它正在工作。
我的新next.config:
const getRoutes = require('./routes');
module.exports = {
trailingSlash: true,
useFileSystemPublicRoutes: true,
exportPathMap: getRoutes,
serverRuntimeConfig: {
// Will only be available on the server side
mySecret: 'secret',
secondSecret: process.env.SECOND_SECRET, // Pass through env variables
},
publicRuntimeConfig: {
// Will be available on both server and client
staticFolder: '/static',
},
}
推荐阅读
- python - 使用 python (Raspbery Pi) 在 excel 中记录信息
- robotics - pcl::visualization c++ api 是否支持用点云显示 2D 图像?
- javascript - 为什么此文件注册脚本中未定义“交互”参数?
- python - 线性回归函数试图为 LASSO 模型找到最佳 lambda 值.. 输出错误
- c++ - 尝试使用 c++ 库时出现奇怪的错误
- c# - 如何在 .Net/C# 中检测 Websocket 断开连接?
- google-sheets - 如何使用多行进行查找
- javascript - 如何将查询与 MongoDB 运算符结合起来?
- reactjs - 如何限制自动完成输入 / ant.design 的结果数量
- python - 主函数使变量未定义?