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

查看:14
本文介绍了如何将我的 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 或者如果我从我的 wifi 网络上的另一台设备访问它,然后我通过在控制台中键入 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 got my answer. See if this can help you.

简短回答 这个视频是我的回答.如果您使用的不是 Windows,您还可以查看这个这个,或者再次就在这里

Short answer This video was my answer. If you're not on a Windows, you can also check this one or this one, or again this right here

长答案

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

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

Here is what I came across: 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 上可能不太好的软件(putty、winscp 等).但是这些其他视频更适合 Mac 观众(顺便说一句,腻子的 Linux 替代品是 flightplan,所以如果这些视频不能帮助你的 Linux 用户,只需快速谷歌搜索像部署 nodejs 飞行计划"这样的词)

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')

轶事 在我更改了在 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 连接,则可能必须从客户端启动连接.做 var socket = io.connect(); 就足够了!您不需要传递 'localhost:3000' 的东西,因为默认情况下它会遵循 localhost 路径.

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(); 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 and wordy... I just hoped someone would have broken this down for me in a similar way when I was struggling to find an answer!

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

Anyways, I hope this is useful to someone!

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

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