在 localhost:3000 上运行的 Nodejs 应用在 Linux 服务器上无法访问

时间:2021-06-13 11:06:06

标签: node.js centos

我想在云服务器上部署我的 web 应用程序,操作系统是 Centos 7,我得到了一个静态 IP 地址,如“34.80.XXX.XX”,如果我将我的 web 应用程序设置为在端口 80 上运行,我可以看到输入“34.80.XXX.XX:80”时的页面。

但是如果我在端口 3000 上运行,并输入“34.80.XXX.XX:3000”,则它不起作用。

我试图停止我的防火墙:

# systemctl stop firewalld

并使用下面的命令检查端口 3000 上没有其他程序运行

# netstat -tnlp

这是app.js中的代码

const Koa = require('koa')
const json = require('koa-json')

const app = new Koa()
const PORT = 3000

// make JSON Prettier middleware
app.use(json())

// Simple middleware example
app.use(async ctx => {
    ctx.body = {msg: 'Hello World'}
})

app.listen(PORT)
console.log(`server run at http://localhost:${PORT}`)

我使用 pm2 在后台运行它

[root@instance-1 01-hello]# pm2 start app.js -n hello
[PM2] Starting /home/xing/01-hello/app.js in fork_mode (1 instance)
[PM2] Done.
┌─────┬──────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name     │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │  
├─────┼──────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤  
│ 0   │ hello    │ default     │ N/A     │ fork    │ 3835     │ 0s     │ 0    │ online    │ 0%       │ 12.6mb   │ root     │ disabled │  
└─────┴──────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘  

然后我输入命令 netstat -tnlp 来检查 app.js 是否在端口 3000 上运行

[root@instance-1 01-hello]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      15958/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1184/master
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      2421/mongod
tcp6       0      0 :::22                   :::*                    LISTEN      15958/sshd
tcp6       0      0 :::3000                 :::*                    LISTEN      3835/node /home/xin
tcp6       0      0 ::1:25                  :::*                    LISTEN      1184/master

这个问题纠结很久了,有什么好的解决办法

0 个答案:

没有答案