简体   繁体   中英

pointing node.js app to my ip address and running locally

So I have two questions about the topic. I have purchased a VPS with digitalocean.com to host my node.js app and they gave me an example hello world app to start.

var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
var html = "hello world";
res.end(html);
}).listen(8080, 'localhost');
console.log('Server running at http://localhost:8080/');

So this code works locally and when I host on the VPS I replace 'localhost' with the IP address of my VPS and it is available on the web when I go to http://IP_Address_of_VPS:8080

My first question: How do I host it live on the web but have it point to my IP address? When I replace 'localhost' with my IP address it does not work when I go to http://my_IP_Address:8080

My second question: I made a test app following a node.js tutorial and here is the code

var PORT = process.env.PORT || 8080;
var express = require('express');
var app = express();
var http = require('http').Server(app);

app.use(express.static(__dirname + '/public'));

http.listen(PORT, function(){
console.log('listening on port *:'+PORT);
});

Where public is the folder with a simple html page. This app works when I run it locally at http://localhost:8080

Can someone explain how the second app runs locally when I did not specify 'localhost' in the http.listen() function? Also how do I point the second app to run from my IP address if the process is any different from the first?

First question

I hope I understood your question right, here is an attempt at an answer :)

Depends a bit on what you mean with 'my ip address'. If it is your public ip adress, you might have to set up port forwarding. Most routers will deny traffic from the outside network (which you are doing when connecting to your own public ip address) to the internal network by default.

If you mean a private ip address (local to the local network) you need to you use your public ip address and set up port fowarding.

When you set up port forwarding to your local machine you might also want to make your private ip address static.

However, I do not recommend using your home computer as a production server. You will need to solve a lot of problems like making your network secure and having low downtime.

Second question

When no hostname is passed to http.listen , the server will accept connections on all adresses. See documentation for more information .

This works, because when you omit arguments in a function call, they will default to undefined . For example, http.listen(8080) might appear as http.listen(8080, undefined, undefined, undefined) . Inside the function body these will often be substituted with some default value.

The process of making your app available to the rest of the world should not be any different.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM