端口号在Node.js中的表示

huangapple go评论90阅读模式
英文:

Port Number In Node.js

问题

我正在学习如何使用http模块在Node.js中创建服务器。尽管我正在努力理解如何确定需要使用哪个端口号。我应该如何找到我的端口号,还是可以随便选择一个数字?它在所有设备上都不同吗?有没有可以选择的列表?

我对这个话题非常困惑,有人可以解释一下吗?

英文:

I am learning how to create servers in Node.js using the http module. Although I am struggling to understand how to figure out what port number I need to use. How am I supposed to find my port number or can I just use any number? Is it different across all devices? Is there a list I can choose from?

I am very confused about this topic can someone please explain?

答案1

得分: 1

你可以基本上选择监听任何你想要的端口,只要满足以下条件:

  1. 没有其他进程已经在监听它
  2. 端口0-1023是“受限制的端口”。在基于Unix的操作系统上,你需要超级用户权限来绑定它们,而在其他操作系统上,这被认为是一种不好的做法。

Node.js应用程序通常会监听端口3000或4000,但这只是约定,你不必遵循它们。

英文:

You can essentially pick to listen on whatever port you wish, provided that:

  1. No other process is already listening on it
  2. Ports 0-1023 are "restricted ports". On a unix-based operating system you'll need superuser privileges to bind them, on other operating systems it's just considered a bad practice.

Node.js applications often listen on port 3000 or 4000, but those are just conventions, and you don't have to follow them.

huangapple
  • 本文由 发表于 2023年1月8日 03:58:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75043384.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定