Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hosting Node app on my machine

So I've got a node app that I can access via localhost:someportnumber. How do I expose this app publicly (i.e. to the rest of the interwebz, not just my local network) using only my machine, and not some sort of hosting service. Machine is a macbook pro, not sure it matters.

Note - this is purely for understanding, I'm not looking to permanently host a toy webapp on my laptop.

Here's my confusion - my node.js application IS A WEBSERVER, why do I need another server (i.e. one provided by heroku, aws, etc) to host my app? I feel like I'm missing something...

like image 225
logic_ Avatar asked Jan 28 '16 08:01

logic_


3 Answers

Assuming this is a home network, then you likely have some sort of router/firewall that connects your home network to the internet. By default that router blocks all incoming connections to the network (for security reasons).

If you want, you can set up what is called a port forwarding rule where you tell the router that you want any incoming connections on port 80 (which is the default http port from a browser) to be routed directly to your macbook pro on your home network. Usually, you would do that by discovering what the IP address is of your macbook pro when it's on your home network and you would set up the port forwarding rule to that IP address. A port forwarding rule would usually be configured through some sort of web-based admin user interface that you can use from a web browser. You'd have to see the manual for your particular router to see how best to connect to it and configure it.

Unless you set your macbook pro to have a static IP address, its IP address will be dynamically assigned by the router. It will likely stay the same for a little while, but can change over time.

Once you've done this, you then need to discover the IP address of your home web connection. This will likely also be dynamically assigned, but also should stay the same for awhile. One you know this IP address, then a browser out on the interwebs can connect to your web server by going the URL http://xxx.yyy.zzzwhere xxx.yyy.zzz is the IP address of your home network as seen from the interwebs.

If you want to be able to connect to it with a normal domain name such as http://mysampledomain.com, then there are some dynamic DNS services that you can pay for that will set up a domain to your dynamic IP address, though this is not recommended for any long term use. Besides probably being against your ISP's rules, it's not a particularly scalable or robust solution. Moving to a hosting provider would make more sense for the long term.

Here's my confusion - my node.js application IS A WEBSERVER, why do I need another server (i.e. one provided by heroku, aws, etc) to host my app? I feel like I'm missing something...

You don't need another web server. If you were to find a hosting provider, you would use a hosting provider that is good at hosting node.js servers. With those type of providers, there would likely not be another web server - your node.js server would be the only one involved. They might have proxies or load balancers that they use to make a multi-tenant environment work, but you should largely be unaware of those other than making sure you follow the proper installation instructions to get your node.js server up and running properly in their environment.

like image 151
jfriend00 Avatar answered Nov 19 '22 18:11

jfriend00


You don't need a server to host the app - you need a server to be accessible over the internet. There are many ways to do this.

For me to reach your webserver app, I need to know the IP address of your computer. Let's say you can give me your public IP address (find this out at What Is My Ip, give the address to me, and let me to which port to connect to.

Eg. IP address of stackoverflow is 104.16.34.249, but you may have IPv6 only address, regardless. Now, keep in mind that most of home ISPs have a habbit of resetting your public IP address every day.

Next thing, you give me your app port, e.g. 8080.

So I would try to get https://104.16.34.249:8080/ (or http://104.16.34.249:8080/, depending on your app).

But I would, in most scenarios, hit either nothing or your router. So that's step 2: tell your router to pass traffic on this port up to your computer. You can have a simple port forward config, or eg. put the computer in a DMZ. (How to do all this is another question, probably over on ServerFault or SuperUser).

So, now I can access your app from the interwebz.

But you probably want me to use some constant name, not everchanging IP address. So you register for a service like DynDNS or No-IP and install their little snippet on your computer or your router, and they give you something like a home hostname, eg. http://best-home-webserver-that-will-never-run-out-of-electricity.dyndns.com/ or http://my-cool-app.no-ip.com/.

Talk about stepping up your game, huh?

But what if you want a "real" domain name, your own, like "http://i-have-webz.com:8080/"? Well, your next step is to buy this domain from somebody like Namecheap, and then set your DNS to point to either your IP address (if it's static - that is, if it doesn't change all the time), or as an alias to your no-ip/dyndns (dynamic) hostname.

The next good step would be something like getting a proper UPS, also a diesel generator for when you run out of power, renting another DSL line from a different provider on a different loop (so you have eg. one cable and one fiber line) for backup or maybe borrowing wireless access from your neighbor for this purpose, implementing round-robin dns stuff, growing a huge business out of it then move it all to Amazon.

What's described above are high-level steps for just one way to handle your scenario. It's not a very complete way, not exhaustive in possible pitfals. There are many more, probably better ways to do many or all steps.

like image 20
Zlatko Avatar answered Nov 19 '22 18:11

Zlatko


you can use nginx and add configuration on your nginx proxy to your node js it's example configure with nginx

server {
 listen 80;

server_name example.com;

location / {
    proxy_pass http://APP_PRIVATE_IP_ADDRESS:8080;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
 }
}
like image 44
Aditya Budi Utomo Avatar answered Nov 19 '22 19:11

Aditya Budi Utomo