Using Azure ZipDeploy, what port should my Node.js app listen on? - node.js

I have a GraphQL, Node api layer that by default, listens on port 3001.
When I deploy the app to an Azure Web App using ZipDeploy, I can't access any endpoints (e.g. /graphql).
I understand that Azure Web Apps only listen on 80 or 443, but I get an error when I configure my app to listen on either of those.
2019-06-04 10:09:40 [32minfo[39m: Running a GraphQL API server at
http://localhost:443/graphql
events.js:167
throw er; // Unhandled 'error' event
^
Error: listen EACCES :::443
at Server.setupListenHandle [as _listen2] (net.js:1286:14)
at listenInCluster (net.js:1334:12)
at Server.listen (net.js:1421:7)
Is there something else in Azure I need to configure?
Thanks,
Jeff

If you're deploying to an Azure Web App, only ports 80 and 443 are public-facing. This maps to a specific port for your app to listen to, retrievable via process.env.PORT.
And if you're running both in Azure and locally, you can easily manage which port you listen to, with something like this, based on your reference to port 3001 in your question:
var port = process.env.PORT || 3001;
server.listen(port);

Related

Node.js: Making Hello World page public

I copied the Hello World example from
https://nodejs.org/en/about/
and it works fine on my Ubuntu cloud instance. Now I'd like to make the Hello World page visible to the entire Internet. What changes are required to the code to accomplish this?
Update: When port is set to 80 and hostname is set to the instance IP address, the following errors are generated when attempting to initiate a node.js session:
ubuntu#instance04:~/NodeJS/NodeHW$ node index01
events.js:183
throw er; // Unhandled 'error' event
^
Error: listen EACCES <my_ip_address>:80
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at Server.setupListenHandle [as _listen2] (net.js:1334:19)
at listenInCluster (net.js:1392:12)
at doListen (net.js:1501:7)
at _combinedTickCallback (internal/process/next_tick.js:141:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
at Function.Module.runMain (module.js:686:11)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
You should be able to view the page on your server using its IP. But you may need to take a couple of steps first:
Take note of which port your Node app is listening on. 3000? 80? In production (i.e. on your cloud server instance), it would be customary to run your application on the default HTTP port, which is 80. But other ports are fine to use too.
Depending on your cloud provider, you may have to adjust its Network Settings before it allows any inbound traffic from the internet through to your server. On AWS, for example, each instance is bound to a Security Group. Think of this as a cloud-level firewall that controls internet access to and from your server. On AWS, again as an example, you'd have to adjust your Security Group Inbound Rules to allow TCP connections to be established to port 80 (or whichever port your Node app is listening on). Here's a sample picture of what it looks like. In this example, I've opened port 80 of my server for TCP traffic. Open whichever port that your Node app is listening on. The protocol for HTTP is TCP.
Furthermore, you may have to also adjust your server's internal firewall settings. On Ubuntu, in order to check the status of the firewall, issue this command:
$ sudo ufw status
If the status is inactive, then it means Ubuntu is not enforcing any firewall rules. If it's active, however, you need to make sure that it is allowing incoming traffic to your Node app's port. I'll let you research how to adjust ufw settings.
Finally, obtain the Public facing IP address or assigned domain name of your server. On AWS, this information is available on your instance details view:
So, now you should be able to browse, from anywhere on the internet to your server's public IP and your Node app's port and view the page.
http://ip:port
If your server's IP is 123.123.123.123 and your Node app is listening on port 3000, then the address would be http://123.123.123.123:3000. If your Node app is listening on port 80, then there's no need to specify the port when you browse. So you can simply go to: http://123.123.123.123
I received the answer from tech support at DreamHost.com. I quote: "The problem is that in order to bind to any port lower than 1000, you need to use sudo."
I am using port 80, so the following works:
sudo node index01
Update: The exact limit is 1024, i.e., when a port lower than 1024 is used sudo must precede the node command.

Webpage not publicly accessible via port 80

I have the following code written with Node/Koa, which is serving to port 80:
const
koa = require('koa'),
route = require('koa-route'),
network = koa(),
common = require('koa-common'),
PORT = 80;
// enable logger middleware
network.use(common.logger('dev'));
// enable static middleware
network.use(common.static(__dirname + '/public'));
network.use(route.get('/', index));
network.use(route.get('/about', about));
function *index() {
this.body = "<h1>Is this message on my computer, or on yours...?</h1>";
}
function *about() {
this.body = "<h2>How about now...</h2>";
}
var server = network.listen(PORT, function () {
console.log('Listening on port %d', server.address().port);
});
I have reserved an address (168.192.1.91) for the host computer, set up port forwarding to this address on port 80, made an exception in the Windows 10 firewall for port 80 when connected to via any protocol, and tested with You Get Signal:
which confirms that the port is currently open. When I browse to localhost:80 I can see the default page. However, when I type the computer's public IP address into the browser (I'm typing in the one that I've partially obscured, which I believe should be the correct one):
this page fails to load with the following error:
This site can’t be reached
109.[...] took too long to respond.
Try:
Reloading the page
Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_TIMED_OUT
and there is no activity in the Koa log (which logs fine when I browse to there via localhost:80). Any ideas what could be blocking the connection?
I have also tried adding the host address as a second parameter like this:
const HOST = "127.0.0.1";
var server = network.listen(PORT, HOST, function () {
console.log('Listening on port %d', server.address().port);
});
This works for the loopback address, but when I specify my public IP I get this error:
C:\Sites\order-server>node --harmony cheese.js
events.js:154
throw er; // Unhandled 'error' event
^
Error: listen EADDRNOTAVAIL [My public IP here]:80
at Object.exports._errnoException (util.js:890:11)
at exports._exceptionWithHostPort (util.js:913:20)
at Server._listen2 (net.js:1221:19)
at listen (net.js:1270:10)
at net.js:1379:9
at _combinedTickCallback (node.js:386:13)
at process._tickCallback (node.js:407:11)
at Function.Module.runMain (module.js:449:11)
at startup (node.js:142:18)
at node.js:939:3
Maybe koa by default only binds to 127.0.0.1 so you can try binding to any host explicitly with .listen(80, '0.0.0.0')
Otherwise if the firewall ant the port forwarding is configured accordingly, the problem could be that your Internet Provider is blocking incoming connections somewhere. You could try useing an Port over 1024, if it only blocks Ports below.

Run first Nodejs app on cpanel godaddy hosting

I'm trying to run my first Node.js application, but I'm having trouble. This could be an error with the firewall on CPanel, but I'm not sure.
I'm running Node.js version 5.0.0
And this is my js:
var net = require('net');
var server = net.createServer(function (socket) {
socket.write('Open Serverrn');
socket.pipe(socket); });
server.listen(674, 'my.ip.add.ress');
console.log('Server running at http://my.ip.add.ress:674/');
And final : this is my notification :
node test.js
Error is:
Server running at http://my.ip.add.ress:674/
events.js:141
throw er; // Unhandled 'error' event
^
Error: listen EACCES my.ip.add.ress:674
at Object.exports._errnoException (util.js:860:11)
at exports._exceptionWithHostPort (util.js:883:20)
at Server._listen2 (net.js:1221:19)
at listen (net.js:1270:10)
at net.js:1379:9
at doNTCallback3 (node.js:461:9)
at process._tickCallback (node.js:367:17)
at Function.Module.runMain (module.js:459:11)
at startup (node.js:136:18)
at node.js:972:3
When I open my console with port 674, it's always loading and timing out after a few seconds. Why is that? Plz help me this issue.
I do not believe GoDaddy has Node.js support, as per this: How to install nodejs application in Godaddy server
Common hosts for Node apps would be:
Digital Ocean, Nodejitsu (owned by GoDaddy anyway), Modulus, Heroku, Joyent, and AWS, I believe.
You need to use port higher than 1024. To bind lower ports you need root privileges or allow program to bind via setcap - can't be implimented at GoDaddy or other shared hosting.

I currently run my amazon ec2 instance on port 3000. I want to run it on port 80 instead. How can I do it?

I have a node.js app that runs on port 3000. I deployed it on amazon web services (ec2) and it works over there. My server.js file says:
var port = process.env.PORT || 3000;
(...)
app.listen(port);
console.log('App listening on port ' + port);
My security group in aws settings seems to have the port 80 also opened:
so I thought it's enough to just change the var port to = 80 and restart the server. But when I did that I got an error:
bitnami#ip-172-31-47-102:~/apps/myproject/www/myproject$ sudo node server.js
App listening on port 80
events.js:141
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::80
at Object.exports._errnoException (util.js:856:11)
at exports._exceptionWithHostPort (util.js:879:20)
at Server._listen2 (net.js:1237:14)
at listen (net.js:1273:10)
at Server.listen (net.js:1369:5)
at Function.app.listen (/opt/bitnami/apps/myproject/www/myproject/node_modules/express/lib/application.js:542:24)
at Object.<anonymous> (/opt/bitnami/apps/myproject/www/myproject/server.js:43:5)
at Module._compile (module.js:398:26)
at Object.Module._extensions..js (module.js:405:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Function.Module.runMain (module.js:430:10)
at startup (node.js:141:18)
at node.js:1003:3
I'm using the Bitnami MEAN 3.2.1-0 system on Amazon.
Also, the reason why I want to change this port is this:
so far all my webservices operate on port 3000. However, I also have there a public_html folder with the index.html file. So when any user wants to display my webpage he has to enter not only the webpage, but also the port (3000) which is not that convenient.
So far the whole app stays under www.ec2-some-random-amazom-numbers.eu-west-1.compute.amazonaws.com:3000/index.html so I will buy a normal top level domain to point at it (eg. something.com ) but then - do I still need to change the port 3000 to 80 in that case? Or maybe it's common to leave apps on port other than 80?
If the latter, then will it be possible for me to leave the port as it is and just point the top level domain on this whole long amazon one with a port 3000 at the end?
So for example: when user types www.something.com it will redirect him to
www.ec2-some-random-amazom-numbers.eu-west-1.compute.amazonaws.com:3000/index.html ?
Something like this should work for you:
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000
You want to use iptables to forward request coming in on port 80 to port 3000 internally.
Based on the error you're getting, (EADDRINUSE), some other web server is listening on port 80 already on your server. If you can prevent that server from running, then you can change your app to run on port 80.
Do the following:
Figure out what's already running on port 80 on your server. There's a good chance it's Apache. Try using netstat to verify.
Kill it (and prevent it from restarting). This will depend on whats listening.
Move your app to port 80 just like you've already tried.
Additional resources:
http://www.cyberciti.biz/faq/what-process-has-open-linux-port/
If you are using the Bitnami Mean Stack then Apache is listening in port 80, hence the conflict. You can either stop the bundled Apache:
sudo /opt/bitnami/ctlscript.sh stop apache
Or you could add a ProxyPass rule in the Apache configuration:
https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#proxypass
I use nginx proxy server for port forwarding
I have faced the same issue.
The solution is to start your NodeJs or ExpressJs app using Port 80, I mean
var port = 80 and start the app using sudo node bin/www
My issue is not able to access the app from internet when the app is running on port 3000: NodeJs App in AWS using port 3000 is not accessible from Internet

Openshift - port to use on deployment

I have the following start.js file:
var express = require('express');
var app = express();
app.use(express.static('static'));
var server = app.listen(8080, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
In my NodeJs application on Openshift. However, when I run rhc tail-a app-name
I can see that there is an error of :
Error: listen EADDRINUSE :::8080
I've tried 80 and 443, and received those errors:
Error: listen EACCESS 0.0.0.0:443
Or 80
Which port should I use as default on my app?
Thanks!
Use Nginx,
Nginx (pronounced "engine x") is a web server. It can act as a reverse proxy server for HTTP, HTTPS, SMTP, POP3, and IMAP protocols, as well as a load balancer and an HTTP cache.
It isn't good practice to run your application with root privileges or directly run your application on port 80 and your port 8080 is in use. Try different port and use reverse proxy.
But if you want to run on port 80 or 443, run your application with root privileges.

Resources