我的项目具有以下结构。
server.js
/node_modules
/build
vendor.js
main.js
index.html
我正在使用express和AngularJS的ui-router
。
在 server.js
中,我有以下代码:
app.get('/*', function(req, res) {
res.sendFile(__dirname + '/build/index.html');
});
在本地,如果我通过 localhost:8080/hellothere
,它就会工作,我会得到正确的页面,但当我部署后,它不会。
在应用程序中,如果使用链接导航运行的路由,则 URL 会修改为 /hellothere
,但如果我更新页面或尝试直接转到我得到的绝对 URL:
Cannot GET /hellothere
我已经尝试过:
app.get('/page1', function(req, res) {
res.sendFile(__dirname + '/build/index.html');
});
app.get('/page2', function(req, res) {
res.sendFile(__dirname + '/build/index.html');
});
...
其行为与'/*'
相同。本地很好,云端就坏了。
知道这里发生了什么吗?
最佳答案
And locally it is working if I pass
localhost:8080/hellothere
, I get the correct page but when I've deployed, it doesn't.
可能的原因是部署后您的基本网址可能是 domainname.com/appname
并且您的本地代码适用于 localhost:portNumber
而不是 localhost:portNumber/appname
所以修改你的路径
app.get('/appname/hellothere', function(req, res) {
res.send("hellothere is working");
});
而不是
app.get('/hellothere', function(req, res) {
res.send("hellothere is working");
});
要在本地运行它,现在您需要 localhost:portNumber/appname/hellothere
关于javascript - ExpressJS 无法在 Openshift 中获取/路由,但在本地可以正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33642500/