Javascript 无法在ec2上初始化Node.js
这是我第一次体验Node.js 使用安装了Node的ubuntu ec2实例,我将遵循本教程: 我的Javascript 无法在ec2上初始化Node.js,javascript,node.js,Javascript,Node.js,这是我第一次体验Node.js 使用安装了Node的ubuntu ec2实例,我将遵循本教程: 我的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({ ex
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)
有人知道我做错了什么吗?如评论中所述,端口
22
需要root权限,并且通常为SSH保留
尝试将const-port=22
替换为const-port=8080
(port8080
是HTTP备用端口)
1024上任何未使用的端口都应该工作(请参阅)。请检查端口80的配置设置:将端口更改为22以外的其他端口。例如,将其设置为8080。如果没有根访问权限,则无法使用端口22
。将端口更改为其他端口
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