【问题标题】:Nextjs with express 404 problem in production modeNextjs 在生产模式下出现 express 404 问题
【发布时间】:2020-06-26 03:51:00
【问题描述】:

我用 express 创建了一个 Next.js 项目。我在 server.js 中定义了一些带有参数的路由,它在开发中正常工作,但是在我导出项目后,当我在该路由中刷新页面时处于生产模式。我从浏览器收到 404 找不到页面错误。 对于生产我执行以下命令 1.npm 运行构建 2.npm运行导出

您可以在下面看到我的文件;

server.js

const express = require('express');
const next = require('next');
const port = process.env.PORT || 3001;

const dev=process.env.NODE_ENV !=='production';
const app = next({dev});
 const handle = app.getRequestHandler();
 app.prepare()
 .then(()=>{
const server=express();

server.get('/', (req, res) => {
  return app.render(req, res, '/index')
});
server.get('/tour/:num/:name', (req, res) => {
  return app.render(req, res, '/tour', { name: req.params.name,num: req.params.num  })
});
  server.get('*',(req,res)=>{
     return handle (req,res);
});
server.listen(port,(err)=>{
    if(err) throw err;
    console.log(`READY ON  http://localhost:${port}`);
})
 });

package.json中的脚本

  "scripts": {
    "dev": "node server.js",
    "build": "next build",
    "export": "next export",
    "start": "node server.js"
  }

next.config.js

中的模块.export
module.exports = withPlugins([[withCSS],[withSass],[withFonts],[withImages]],{ hmr: false },
    {
        exportPathMap: function() {
            return {
              '/': { page: '/' },
              '/tour': { page: '/tour/:num/:name' }

            };
          }
    }

    );

链接来自下一个/链接

   <Link href={{ pathname: "/tour", query: { name:tourName , num:pkgNum} }} as={`tour/${pkgNum}/${tourName}`}>
     <a >
      Tour Details
     </a>
   </Link>

【问题讨论】:

    标签: node.js reactjs express next.js server-side-rendering


    【解决方案1】:

    npm run buildnpm run start

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-20
      • 1970-01-01
      • 2023-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多