如何为 node.js 服务器分配域名? [英] How to assign a domain name to node.js server?

查看:47
本文介绍了如何为 node.js 服务器分配域名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

http://42.12.251.830:4000

我买了一个域名

www.mydomain.com

如何将其分配给我的服务器?首先我使用转发,但后来我不能再使用 location.hash 向 URL 添加聊天频道.然后我使用了类似 Header-Redirect 的东西.现在该服务可在 mydomain.com 下访问,但在 www.mydomain.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 mydomain.com , but not under www.mydomain.com. Moreover the domain name is not shown in the browser window. For my chat channel I need something like this:

www.mydomain.com/#238husd4

推荐答案

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

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 app is listening on the correct port, which on most servers is 80

使用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);

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

now getting a domain name to point to this ip adress is an entirely separate matter. You need to make your name server point to the ip. Your name server will usually be the company you bought the domain name through, for instance GoDaddy is 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.

这篇关于如何为 node.js 服务器分配域名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆