Making my Node app accessible to the Internet from a local machine - node.js

OK so I know I can use cloud hosting and I've done so before but I am doing a demo and I want my node app to be on my local machine but accessible from the internet. Here is how I start the server in the server file
const port = 8080;
var server = http.createServer(app).listen(port, () => {
console.log(`Server listening on port ${port}`);
});
Next I set up port forwarding on my Xfinity gateway such that both port 80 and port 8080 point to my desktop. I know I am connecting to the right device because SSH works from outside of my network on port 22. However when I enter [public IP]:8080 I am unable to receive a response. The only time I receive a response is when I enter 10.0.0.58:8080 which refers to my internal network. Why is this???

I personally like ngrok to do the same thing. It's really easy to setup and I found it really stable.
Give it a go https://ngrok.com
After installing you can simply forward ports like
ngrok http 8080

Related

Can't Access Node.JS over internet

I'm trying to access a simple node.js/express application over the internet, but I can't for the life of me figure out why it's not working. I can access it using http://localhost:3000 and http://192.168.x.x:3000 but not using my external IP address.
Port 3000 is open on my router (double checked with port online port checker tools), and I've added a rule in the firewall to allow the port (Windows 10).
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, "0.0.0.0")
netstat seems to suggest that port 3000 is allowed through the firewall, right?
C:\WINDOWS\system32>netstat -n -a
Active Connections
Proto Local Address Foreign Address State
TCP 0.0.0.0:3000 0.0.0.0:0 LISTENING
You need to forward that port or make a tunnel to use it over internet . By default the ports are blocked. The problem is not the server is not listening the problem is that it is not discoverable from the outside.
To forward the port, Add the forward rule to your router [More info]
For tunnelling you can use ngrok
By doing that you can connect to the server at http://<your_external_ip>:<forwarded_port>
I finally figured out the issue! It has to do with me testing the connection inside of the LAN on a router that doesn't support hairpinning (see point 2 in this stackoverflow answer). Simply accessing the application on a device outside of my LAN does the trick.

Formatting node apps for AWS deployment

I'm trying to deploy a node.js app on aws EC2 Beanstalk. My problem is, I can't figure out how to move from my localhost testing environment to aws standard. Right now, my app works on port 8081 by using the following code.
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
})
How would I change this server variable to work on an actual domain?
Assuming your intent is to provide a public-facing web application, your code will work as is, albeit with a few caveats:
Currently your server will listen on port 8081. Once deployed to AWS users would have to browse to www.somedomain.com:8081 to reach your application. (Assuming the host instance allows traffic on that port. See below).
If your intent is to have users reach your application at www.somedomain.com - without specifying a port - you'll want the server to listen on port 80 instead.
var server = app.listen(80, function () { ... }
In either case you'll need to ensure that the security group rules for the EC2 host instance allow incoming TCP traffic on the listening port. Likewise, if your EC2 host instance is behind a load balancer you'll need to allow incoming traffic on the appropriate ports there as well.
For something a little fancier, you can try deploying your application to Elastic Beanstalk using Docker and exposing port 8081 in the dockerfile. This way users would still reach it at www.somedomain.com (via http port 80) and you could continue to develop and test locally using port 8081.
One final note: you didn't provide much information about what your application is or how you intend to use it, so I'm making quite a few assumptions based only on the information provided.
This code works great for me with node on Elastic Beanstalk, and allows me to seamlessly switch between localhost and remote development without changing any code:
var port = process.env.PORT || 8081;
var server = app.listen(port, function () {
//server is started!!!
})

nodejs express local ip address

I have a NodeJS app running with express which I'm trying to access at the port 80.
So I have this:
app.listen('80', function () {
console.log('Server started');
});
Going to the browser I can acess it by typing "localhost", "127.0.0.1", both with or without :80 as a port.
My question is how can I access it from another computer?
Whenever I type the IP on the browser, it respond as "bad request, invalid hostname"
Your approach is correct. The Express application should be visible by other computers on the same network.
My best guess is that there is "something" running on your computer that prevents port 80 to expose. Or a conflict with another application that is using port 80 as well. (most unlikely because express cannot run if the port is already in use.).

Express: How do you get the hostname and port that an express server is listening on, WITHOUT waiting for a request

It's fairly straight forward to get the hostname and URL from a request that has been made to an express server. But there seems to be no obvious way to determine which URL an express server is listening on (after it has been started up, but before any requests have been sent).
How do you find out which host and port an express server is listening on? Does express even know which host and port it is listening on?
If you create an server like this:
var server = http.createServer(app);
The server instance actually contains the port and address it's listening. So you could just export it, and call server.address() to get the address and port.
{ address: '::', family: 'IPv6', port: 3200 }
If you want further info about what urls are your routes are routing, try
console.log(router.stack)
then the magic happens. :)

Express ssl address

I am building an express app that will run on my local network. I just started using a self signed certificut and an https server instead of just an http server. Before I implemented https, I could go to my app with 192.168.1.66 (local ip). But after implementing https, I now have to type https://192.168.1.66:80, otherwise my browser says "connection was reset" or something similar.
Below is my server creation code:
var port = process.env.PORT || 80;//is always 80 since I have not
//set process.end.PORT
var server = https.createServer(sslOptions, app).listen(port, function(){
console.log("listening on port 80");
});
Any ideas why?
It looks like this was a firewall issue on linux. I had turned off ufw, but failed to restart afterwards. After restarting and confirming that the firewall was off, it worked fine :)

Resources