如何为 node.js 服务器分配域名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17516810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:58:36  来源:igfitidea点击:

How to assign a domain name to node.js server?

javascriptnode.jsurldns

提问by user2535056

I have a node.ja server listining to port 4000, the URL to access the service is something like this:

我有一个 node.ja 服务器列出端口 4000,访问该服务的 URL 是这样的:

http://42.12.251.830:4000

I bought a domain name

我买了一个域名

www.mychat.com

How can I assign it to my server? First I used forwarding, but then I could not use location.hash anymore to add a chat channel to the URL. Then I used something like Header-Redirect. Now the service is reachable under mychat.com , but not under www.mychat.com. Moreover the domain name is not shown in the browser window. For my chat channel I need something like this:

如何将其分配给我的服务器?首先我使用转发,但后来我不能再使用 location.hash 向 URL 添加聊天频道。然后我使用了类似 Header-Redirect 的东西。现在可以在 mychat.com 下访问该服务,但不能在 www.mychat.com 下访问。此外,域名不会显示在浏览器窗口中。对于我的聊天频道,我需要这样的东西:

www.mychat.com/#238husd4

采纳答案by Loourr

You dont assign a domain to a node.js server, instead you load your app onto a machine which has an ip adress, which in your case is 42.12.251.830:4000. You then need to make sure your appis listening on the correct port, which on most servers is 80

您没有为 node.js 服务器分配域,而是将您的应用程序加载到具有 IP 地址的机器上,在您的情况下是42.12.251.830:4000. 然后,您需要确保您app正在侦听正确的端口,这在大多数服务器上是80

using expressit's as simple as

使用express就这么简单

var app = express()
  , server = require('http').createServer(app)
  , io = io.listen(server);

app.get('/', function(req, res) {
  res.sendfile('./public/index.html');
});
server.listen(80);

now getting a domain name to point to this ip adress is an entirely separate matter. You need to make your name serverpoint to the ip. Your name server will usually be the company you bought the domain name through, for instance GoDaddyis a Domain Name Server (DNS). So if you had a domain name with them, you would go on their site under DNS settings and change the ip adress. Your domain name will then point to your ip adress and should render your node.js app.

现在让一个域名指向这个 ip 地址是完全不同的事情。您需要使您的名称服务器指向该 ip。您的域名服务器通常是您购买域名的公司,例如GoDaddy是域名服务器 (DNS)。因此,如果您与他们有域名,则可以在 DNS 设置下访问他们的站点并更改 IP 地址。然后,您的域名将指向您的 IP 地址并呈现您的 node.js 应用程序。