无法在ec2上初始化Node.js



这是我第一次使用Node.js.

使用我安装了Node的ubuntu ec2实例,我遵循本教程:https://blog.logrocket.com/setting-up-a-restful-api-with-node-js-and-postgresql-d96d6fc892d8/

我的index.js文件如下所示:

const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const port = 22
app.use(bodyParser.json())
app.use(
bodyParser.urlencoded({
extended: true,
})
)
app.get('/', (request, response) => {
response.json({ info: 'Node.js, Express, and Postgres API' })
})
app.listen(port, () => {
console.log(`App running on port ${port}.`)
})

当我到达运行node index.js的部分时,我会得到以下错误:

ubuntu@ip-172-31-87-85:~/riapi$ node index.js 
events.js:183
throw er; // Unhandled 'error' event
^
Error: listen EACCES 0.0.0.0:22
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at Server.setupListenHandle [as _listen2] (net.js:1350:19)
at listenInCluster (net.js:1408:12)
at Server.listen (net.js:1492:7)
at Function.listen (/home/ubuntu/riapi/node_modules/express/lib/application.js:618:24)
at Object.<anonymous> (/home/ubuntu/riapi/index.js:17:5)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)

我发现了这个SO问题:ExpressJS-抛出未处理的错误事件

当我尝试了一些东西,但没有成功时:

ubuntu@ip-172-31-87-85:~/riapi$ netstat -tulnp | grep 22
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -                   
tcp6       0      0 :::22                   :::*                    LISTEN      -                   
ubuntu@ip-172-31-87-85:~/riapi$ ps aux | awk '/node/{print $2}' | xargs kill -9
kill: (20586): No such process

有人看到我做错了什么吗?

如注释中所述,端口22需要root权限,通常为SSH保留。

尝试用const port = 8080替换const port = 22(端口8080是官方的HTTP备用端口(。

1024上任何未使用的端口都应正常工作(请参阅https://unix.stackexchange.com/questions/16564/why-are-the-first-1024-ports-restricted-to-the-root-user-only)。