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

查看:279
本文介绍了如何为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.mychat.com

如何将其分配给我的服务器?首先我使用转发,但是我不能再使用location.hash来添加聊天频道到URL。然后我用了像Header-Redirect这样的东西。现在该服务可以在mychat.com下访问,但不能在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:

www.mychat.com/#238husd4


推荐答案

您不要将域分配给node.js服务器,而是将应用加载到机器有一个ip地址,在你的情况下是 42.12.251.830:4000 。然后,您需要确保您的应用程序正在侦听正确的端口,大多数服务器是 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天全站免登陆