我有节点快速服务器,服务器在Windows系统上正常工作。但是在debian上总是返回index.html。
当我去localhost:port /它的retrun index.html时,index.html加载一些js文件和图像(fav ico) - 这些文件的内容总是index.html的内容...所以..在哪里一个问题?
代码:
const express = require('express')
const path = require('path')
const application = express()
const port = process.env.PORT || 80
const PUBLIC_DIR = 'public'
application.use(express.static(path.join(__dirname, PUBLIC_DIR)))
application.listen(port)
application.use(express.static('client/build')); //use your build path my build path under the root folder is client/build
const path = require('path');
application.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client','build', 'index.html')); //use your build path my build path under the root folder is client/build
});
//handle 404
application.use((req, res) => {
res.send('404: Page not Found', 404)
});
//handle 500
application.use((error, req, res, next) => {
res.send('500: Internal Server Error', 500)
});
console.log(['HTTP server running on ', process.env.HOST, ' / ', port].join(''))
答案 0 :(得分:1)
您将快递实例声明为const application = express()
但是您在第11行使用app.use
。也许将其更改为application.use
会提供更好的结果吗?
答案 1 :(得分:1)
更改以下行
app.use(express.static('client/build')); //use your build path my build path under the root folder is client/build
到
app.use(express.static(path.resolve(__dirname, 'client','build')));
因此app将获得构建文件夹的正确路径,并且不会落入静态资源请求的catch-all路径。