我已将Express/Vue应用程序部署到生产环境中。
我发现在express中同时提供静态文件和api有点困难。
expressfolder/app.js内部
const app = express()
const path = require('path')
const serveStatic = require('serve-static')
...
// index.html and static/css, static/js - bundle made with npm run build
const DIR_DIST = path.join(__dirname, '../../path/to/dist')
app.use(serveStatic(DIR_DIST))
...
app.get('/tests', (req, res) => {
res.send({msg: 'Hello there!'})
})
-
当我访问myapp.com时,我看到了所需的index.html。
-
如果我直接在浏览器myapp.com/tests中键入,我会看到express中的原始消息“hello there”。
-
如果我通过index.html中的链接调用相同的路由,我会在chrome中收到这个错误
(失败)net::err_连接被拒绝
它在我的本地机器上工作,所以我确信它是某种混乱的配置,我没有正确设置。
而且,我不想
/tests
直接:vue路由器应该覆盖它,但这是一个较小的问题。
以前可能有人问过,但已经有一段时间了,我还没有找到解决办法。
谢谢。