如何将我的node.js服务器指向域名? (我也用socket.io) [英] How do I point my node.js server to a domain name? (I also use socket.io)

查看:492
本文介绍了如何将我的node.js服务器指向域名? (我也用socket.io)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的服务器代码:

var static = require('node-static');
var http = require('http');
var file = new(static.Server)();
var app = http.createServer(function (req, res) {
  file.serve(req, res);
}).listen(2013);

然后,我得到了一大堆与socket.io有关的代码。
我也刚刚在gandi.net注册了一个域名。

And then I got a whole bunch more code that has to do with socket.io. I also just registered a domain name at gandi.net.

所以,gandi.net给了我一个输入DNS的选项。
我如何找到正确的DNS从我的电脑进入?

So, gandi.net gives me an option of entering a DNS. How exactly do I find the right DNS to enter from my computer?

我通常运行它是Windows控制台上的node server.js。然后在浏览器的地址栏中,我只需要执行localhost:2013,或者如果我从另一台Wi-Fi网络上的设备进行访问,那么通过在控制台中输入ipconfig来查找ipv4地址。然后我在其他设备中输入该地址,其次是:2013的端口。

How I usually run it is node server.js on the windows console. Then in the address bar in the browser I just do either localhost:2013 or if I'm accessing it from another device that is on my wifi network, then I look up the ipv4 address by typing ipconfig in the console. And then I enter that address in the other device followed by :2013 for the port.

那么我怎么能用自己的域名运行这个服务器?

So how exactly can I run this server with my own domain name?

我知道我会得到一个实际的永久服务器,但现在我只是想知道如何从我的电脑运行这个。

I know I will be getting an actual permanent server, but for now I just want to know how to run this from my computer.

推荐答案

在想知道同一件事几个星期后,我想我已经得到了答案。查看是否可以帮助您。

After wondering about the same thing for weeks, I think I've gotten my answer. See if this can help you.

只是总结一下这个更广泛的受众群体的问题:您已经弄清楚如何在您的计算机上运行node.js。您正在使用命令行启动计算机上的节点服务器,方法是键入node server.js(如果server.js是您命名的包含服务器代码的文件)。您可能在服务器文件中选择了一个端口号(如3000或9000号),以便您可以通过输入localhost:3000或localhost:9000,通过浏览器访问您的工作。或者,您也可以将nodejs应用程序使用某种类型的socket.io连接,以便您在服务器文件中创建一个房间,并且每次打开localhost:3000或localhost:9000(或者您的使用)通过浏览器,它创建一个新的用户,它添加到房间。一切都很好!
现在,你想要的是让网络访问,而不只是你的电脑。因为如果有人使用不同的计算机来输入他们的浏览器'localhost:3000'(或任何端口),他们会看到有关他们的本地计算机的信息,而不是你在你的计算机上建立的真棒的东西,你可以以访问相同的URL。所以你希望把你的工作放在网上,通过像coolnodejsstuff.com这样的域名访问。但是你想知道如何使您的计算机上的server.js代码指向一个域名。写出server.js代码中的域名的URL似乎没有帮助!

Just to summarize the issue here for a more general audience: You have figured out how to run node.js on your computer. You are using the command line to start the node server on your computer, by typing 'node server.js' (if "server.js" is what you named the file containing the server code). You probably chose a port number in your server file (a number like 3000 or 9000), so that you can access your work through your browser by typing localhost:3000 or localhost:9000. Optionally, you may also have your nodejs app use some kind of socket.io connection, so that you're creating a room in your server file, and every time you open localhost:3000 or localhost:9000 (or whichever port you're using) through your browser, it creates a new user that it adds to the room. All that is good! NOW, what you want is to make that accessible to the web, not just your computer. Because if somebody with a DIFFERENT computer were to type in their browser 'localhost:3000' (or whatever port), they would see information regarding THEIR local computer, not the awesome stuff that you have built on YOUR computer and that you're able to access by the same URL. So you wish to have your work put online, accessible through a domain name like coolnodejsstuff.com. But you're wondering how to make the server.js code on your computer point to a domain name. Writing out the URL of the domain name inside the server.js code doesn't seem to be helping!

这是我来的:您需要的是一个将在线运行的服务器(而不是您的计算机)。它类似于当您为您的域名购买托管时,区别在于您申请简单托管时您无法访问服务器的功能。您需要查找的是 专用服务器 ,以便您可以手动安装该服务器上的东西(像nodejs等),就像您可能在安装nodejs时一样在你的电脑上通过命令行。然后,您可以将所有文件上传到专用服务器上,并安装服务器上需要的内容(nodejs等)。nodejs应用程序始终指向运行的机器上的IP地址的localhost。当您获得专用服务器时,它会附带一个IP地址。这就是上传的文件将被指向的地方,这意味着,如果您正确安装所有内容,并正确启动服务器,那么您可以通过浏览器访问您的nodejs应用程序,如果在URL字段中输入专用服务器的IP地址(之后是冒号和您在代码中指定的端口)。
但是您希望人们通过有意义的url访问您的节点应用程序不是一堆数字,您希望应用程序指向一个可以更容易记住的域名。为此,您需要简单地更改您的域名信息(提供您自己的信息),以满足您购买专用服务器的任何需求。

Here is what I came upon: What you need is a server that will be running online (and not in your computer). It's similar to when you buy hosting for your domain name, with the difference that you don't have much access to the server's functionalities when you apply for simple hosting. What you need to look for is a dedicated server so that you can manually install things on that server (things like nodejs, etc.) just like you probably did when installing nodejs on your computer through the command line. You will then upload all your files on the dedicated server and install what is needed on the server (nodejs, etc.) A nodejs app always points to the 'localhost' of the IP address on the machine it's running. When you get a dedicated server, it comes with an IP address. That's where the uploaded files will be pointing, meaning, if you install everything properly, and you start the server properly, you should be able to access your nodejs app through your browser if you enter in the URL field the IP address of the dedicated server (followed by a colon and the port that you specified in the code). But you want people to access your node app through a meaningful url not a bunch of numbers, you want the app to point to a domain name that people can more easily remember. For that, you will need to simply change the information of your domain name (provided you own one) to match the requirements of whoever you bought your dedicated server with.

好的,但是你怎么实际呢?

这里是我得到我的解决方案的视频。它解释得很好,我刚才在上面说过。如果您遵循这些说明,您应该可以在不到一个小时内将您的节点应用程序联机。它主要是为Windows用户使用,因此使用一些可能不如linux(软件,winscp等)的软件
但这些其他视频更适合于Mac用户(和btw,腻子的Linux替代品是 flightplan ,所以如果这些视频无法帮助你的Linux用户,那么只需用deploy nodejs flightplan这样的词来快速搜索google。

Here is the video I got my solution from. It explains pretty well all I just said above. You should be able to get your node app online in less than an hour if you follow these instructions. It's mainly made for windows users therefore uses some software that may not be as good on linux (putty, winscp, ect.) But these others videos are more suitable to a Mac audience (and btw, the Linux alternative for putty is flightplan, so if these videos can't help you Linux users out there, just make a quick google search with words like 'deploy nodejs flightplan')

  • 1.Spin up a server, 2.Provision server & setup flightplan
  • How to deploy a node.js app

Anecdote 我的应用程序在我更改方式我通过我的server.js文件中的端口号,以匹配视频中的示例。
我相信我在说

Anecdote My app didn't work until I changed the way I passed the port number in my server.js file, to match the example in the video. I believe I was saying

var app = express();
var server = app.listen("3000");

在将其更改为

var app = express();
var server = app.listen(process.env.PORT || 3000);

而且,如果您使用的是socket.io连接,则可能需要从客户端启动连接。 data-hide =falsedata-console =truedata-babel =false

And also, if you're using a socket.io connection, you probably have to start a connection from the client side. Doing

var socket = io.connect();

足够了!你不需要传递'localhost:3000'的东西,因为默认情况下它将遵循localhost路径。

is sufficient! You don't need to pass the 'localhost:3000' stuff since by default it will follow the localhost path.

有!你应该设置!我道歉,如果这是一个长时间的一个冗长的话...我只是希望有人会打破这个下来,当我在努力寻找答案!

There! You should be set! I apologize if this is a bit long a wordy... I just hoped someone would have broken this down for me when I was struggling to find an answer!

无论如何,我希望这对某人有用!

Anyways, I hope this is useful to someone!

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

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