ExpressJS无法在Openshift中获取/路由,但在本地它可以工作

ExpressJS cannot get /route in Openshift, but locally it is working

本文关键字:工作 路由 Openshift 获取 ExpressJS      更新时间:2023-09-26

我的项目有以下结构。

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

其行为与CCD_ 5相同。很好的本地,在云上破碎。

知道这里发生了什么吗?

同源网站链接

在本地它是有效的,如果我通过localhost:8080/hellothere,我得到正确的页面,但当我部署后,它没有。

可能的原因可能是在部署后,您的基本url可能是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