【问题标题】:Cannot initialize Node.js on ec2无法在 ec2 上初始化 Node.js
【发布时间】:2020-06-26 06:21:42
【问题描述】:

这是我第一次使用 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 - throw er Unhandled error event

当我尝试了几件事,但没有成功时:

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

有人看到我做错了吗?

【问题讨论】:

  • 请检查端口 80 的配置设置:hackernoon.com/…
  • 将端口更改为 22 以外的端口。例如,将其设置为 8080。
  • 如果没有 root 访问权限,您将无法使用端口 22。将您的端口更改为其他内容

标签: javascript node.js


【解决方案1】:

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

尝试将const port = 22 替换为const port = 8080(端口8080official HTTP 备用端口)。

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

【讨论】:

    猜你喜欢
    • 2019-03-31
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    • 2014-02-03
    • 2016-06-17
    • 1970-01-01
    • 1970-01-01
    • 2022-06-30
    相关资源
    最近更新 更多