How do I point my node.js server to a domain name?

2019-02-25 20:11发布

This is my server code:

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

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.

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

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.

2条回答
啃猪蹄的小仙女
2楼-- · 2019-02-25 20:55

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

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


Long answer

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!

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.

Okay, but how do you actually do it?!

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

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");

It came to work after I changed it to

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

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!

查看更多
We Are One
3楼-- · 2019-02-25 20:59

It should listen on port 80.

And the dns settings of the domain provider should point to your outgoing ip address

查看更多
登录 后发表回答