Node.js服务器可在localhost上运行,但不能在真实服务器上运行

时间:2019-10-22 15:46:29

标签: node.js express

我正在为应用程序设置服务器,我需要接受发送到服务器的请求。 当在localhost上运行节点时,一切正常,但是当我在公司网站上运行代码时,它不会响应。

也许它与地址有关,因为当我在localhost中运行时,它是“ http://localhost:3000/”。 但是,当我在公司服务器上运行时,它是“ http://company.com:3000/app/”。

const express = require("express");
const app = express();
const port = 3000;
app.use(express.json());
app.use(express.urlencoded({extended:true}));

app.get('/', (req, res) => {
        res.status(200).send("Hello");
});

app.listen(port, () => {
        console.log('Server listening on port ' + port);
});

2 个答案:

答案 0 :(得分:0)

如果您同时在本地主机和服务器上的端口3000上运行,并且想要从部署服务器上的/app获取所有请求,则需要相应地更改app.get()

app.get('/app', (req, res) => {
    res.status(200).send("Hello");
});

答案 1 :(得分:0)

您在服务器上设置了防火墙吗? 您是否尝试过打开服务器上的端口? 如果端口关闭,则服务器将无法在本地界面“外部”提供内容。

如果您不想打开该端口,也可以尝试创建反向代理,这意味着网络服务器将从侦听端口3000的进程中获取内容,并将其重定向到指定的端口(可以是80或443)。