当我使用REACT时,我所要做的就是运行NPM Build,将内容复制到Node中名为public
的文件夹中,然后添加以下行,它就可以工作了:
Node/app.js
app.use(express.static(path.join(__dirname, 'public')));
//...routes
app.use((req, res, next) => {
res.sendFile(path.resolve(__dirname, 'public', 'index.html'))
})
一切正常,来自所有链接的内容都按其应有的方式显示,这很好.
然而,在NextJS中运行npm run build
会在.../server/pages
中生成一个index.html文件.
所以我这么做了:
app.use(express.static(path.join(__dirname, 'public')));
//... routes
app.use((req, res, next) => {
res.sendFile(path.resolve(__dirname, 'public', 'server', 'pages', 'index.html'))
})
运行Node应用程序确实会显示index.htm
个文件的内容,但点击其中的任何链接都会重定向到index.html页面,即使显示的URL发生了变化.
求求你,救命!