javascript - ExpressJS 无法在 Openshift 中获取/路由,但在本地可以正常工作

标签 javascript node.js express angular-ui-router openshift

我的项目具有以下结构。

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');
});
... 

其行为与'/*'相同。本地很好,云端就坏了。

知道这里发生了什么吗?

homolog Website link

最佳答案

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/

相关文章:

Node.js/Express.js session management cookie 为 session cookie

node.js - 要求中的双括号是什么意思

node.js - 无法使用 nvm 全局安装express-generator

javascript - 如何显示充满某种颜色或图像的类的前景?

javascript - 如何访问Knockout组件功能

javascript - 为什么 $.namespace() 仍然不是核心 jQuery 实现的一部分?

node.js - 使用 putObject 时 S3 文件损坏

javascript - 当我在 expressjs 中使用 req.param 并在我的后 Controller 中使用 nodejs 时,我得到 UNDEFINED

javascript - OpenShift NodeJS 应用程序 503 错误(端口问题?)

javascript - 如何在关闭 iframe 后关闭父 p 标签?