Why does my express.js app never loads? - node.js

I did everything as specified here:
https://expressjs.com/en/starter/hello-world.html
When I try to reach my domain, and append :3000 to the end, it just never loads (timeout).

If you did everything specified on https://expressjs.com/en/starter/hello-world.html and it didn't work then it must mean that the tutorial is incorrect.
If, on the other hand, you didn't do everything as specified in the tutorial (which we will never know since you didn't post what you actually did) then you should make sure to follow the tutorial more closely because it doesn't look incorrect.
The reasons why trying to reach a random domain on a random port times out can be:
wrong port
wrong domain
bad DNS record
misconfigured DNS resolver
firewall rules
server not listening
server listening on a different port
server listening on a different interface

Unfortunately you didn't provide enough information for a better answer, with that said the most likely issue is that you never actually executed the JavaScript file you created. You'll want to make sure Node is installed and run:
nodejs app.js
Keep in mind that in some distros node doesn't exist, but nodejs does, when installing node from a package manager or other installer.
EDIT
There are other potential issues you'd run into if you don't have port 3000 opened up if you're not running it on localhost.

Related

Unable to deploy an application on Heroku due to some port problems

I've been trying to deploy a repository https://github.com/evelynhathaway/triton-poll to heroku, but since I am fairly new to NodeJs, I am unable to detect the problem. But I guess it's due to the port because heroku doesn't use static ports.
Any help would be appreciated.
Thank You in advance.
I looked at the fork and you did a couple of mistakes. I don't have the time to fix, test and get it to run but I can show you how I solved it before.
All the relevant code changes can be found in this commit (different project):
https://github.com/vegeta897/d-zone/commit/63730fd7f44d2716a31fcae55990d83c84d5ffea
The project is divided into a client and server part.
You can see here, https://github.com/vegeta897/d-zone/blob/63730fd7f44d2716a31fcae55990d83c84d5ffea/script/websock.js#L16, how I combined server and client into one. This only works because the static client files are served via http/https and the server uses websocket, no http ws/wss
When you publish a server on Heroku you need to bind to their dynamic port. However when you want to access the web server you do not specify a port. The hostname is automatically translated into an ip-address + port combo. I did this here: https://github.com/vegeta897/d-zone/blob/63730fd7f44d2716a31fcae55990d83c84d5ffea/web/main.js#L44 When deployed on Heroku the socketURL does not contain a port number.
Finally you bind to the server. I did it here https://github.com/vegeta897/d-zone/blob/63730fd7f44d2716a31fcae55990d83c84d5ffea/script/websock.js#L55 and here https://github.com/vegeta897/d-zone/blob/63730fd7f44d2716a31fcae55990d83c84d5ffea/socket-config.js#L30
You also have to make sure that your clients files are built properly and served.

linux redirect localhost port to url port

I need to redirect localhost:8080 to http://url:8080/.
Some background:
I am using docker swarm stack services. One service (MAPS) creates a simple http server that lists xml files to port 8080 and another service (WAS) uses WebSphere Application Server that has a connector that uses these files, to be more precise it calls upon a file maps.xml that has the urls of the other files as http://localhost:8080/<file-name>.xml.
I know docker allows me to call on the service name and port within the services, thus I can use curl http://MAPS:8080/ from inside my WAS service and it outputs my list of xml files.
However, this will not always be true. The prod team may change the port number they want to publish or they might update the maps.xml file and forget to change localhost:8080 to MAPS:8080.
Is there a way to make it so any call to localhost:8080 gets redirected to another url, preferrably using a configuration file? I also need it to be lightweight since the WAS service is already quite heavy and I can't make it too large to deploy.
Solutions I tried:
iptables: Installed it on the WAS service container but when I tried using it it said my kernel was outdated
tinyproxy: Tried setting it up as a reverse proxy but I couldn't make it work
ncat with inetd: Tried to use this solution but it also didn't work
I am NO expert so please excuse any noob mistakes I made. And thanks in advance!
It is generally not a good idea to redirect localhost to another location as it might disrupt your local environment in surprising ways. Many packages depend on localhost being localhost :-)
it is possible to add MAPS to your hosts file (/etc/hosts) giving it the address of maps.

Debugging with node-inspector: Cannot GET /

I am running a Nodejs server with an application which I want to debug.
In order to achieve this using node-inspector I run the app as follows:
node-debug server.js
Unfortunately I can not access the webserver via URL anymore. Visiting http://127.0.0.1:8080 results in
Cannot GET /
However if I start the application the usual way with
node server.js
everything is fine (except for the fact that I can not debug). But I can access http://127.0.0.1:8080.
The '/' request is also not logged so it seems that it never reaches the server.
Hence the problem I have is: I can access the remote debugger via http://127.0.0.1:8080/debug?ws=127.0.0.1:8080&port=5858 but I can not start debugging because I can not trigger any action on the webserver via URL.
Oh and the debugger is not paused or anything. I skipped the first break point.
I resolved the problem by choosing a different port than 8080. It seems like the debugger uses this port. I was not aware of that because this port was familiar to me as I used it for my application.
As Schnodderbalken already answered, the problem lies in the application and inspector both using the same port 8080. If you tell inspector to use a different web port, 8082 for instance, the problem is solved.
You can do this by adding a parameter like so:
node-inspector --web-port=8082
You can then access inspector via http://127.0.0.1:8082/?port=5858

Why does ec2 instance not display my website? Using nodejs

I am running a m1.microinstance of aws, using CentOS. I downloaded Yeoman, git, npm and all of the dependencies are present. I am trying to run a MEAN stack on this server, so, mongo, express, angular and node. However, when I visit my public DNS, my site gives me this error: "Oops! Google Chrome could not connect to ec2-54-191-0-63.us-west-2.compute.amazonaws.com". On my admin control panel, I see my instance status, and it says it is running. I understand that if I had used apache, the page that displays is in the /var/www/html directory.. So, how do I get a directory similar to apaches, to display my html files, or whatever I would like the public to see? I have my security groups configured, for inbound, to listen to SSH port 22, for everyone, as well as HTTP port 80, for everyone.
Yeoman set up a nice app folder for me, but for some reason it does not display. I thought maybe I was missing a server.js, but that does not seem to have fixed anything when I added it. Any advice? Thanks!
Make sure you are matching the port all the way through - your browsers URL:PORT, the EC2 routing rules and your NodeJS settings. It looks like you might be listening to a port higher than 80 on the server.
As you mentioned in your comment, if you want to listen on a port below 1024 you will need to run the command as a privileged user.
I didn't run node as root on my AWS server, so it was not setting up my nicely built app that Yeoman made for me.
http://www.stackoverflow.com/questions/9164915 was where I realized my mistake. I am new to linux OS soo, I am learning. :)

Why is running node.js on port 80 might not be safe?

I'm aware that nodejs can run on the port 80 by :
setting up a proxy with apache/nginx
setting up a port redirection with an iptables rule
making express listen on port 80 within the app
The first option isn't the easiest and require more dependencies then I need. The second one could be working but on my OpenVZ VPS it won't (and I can't compile a custom linux kernel).
I needed to handle some sub-domains too, and after reading this stackoverflow answer, I tried the third solution.
It's perfectly working and very easy to perform.
I was wondering if there might be some security issues while running nodejs directly on the 80 port ? How may I fix/found these ?
I wanted to use pm2 to handle the processes and it might run not as root (Goodbye node-forever hello pm2).
The first option isn't the easiest and require more dependencies then I need
Please review why should one use a http server in front of a framework web server for the many valid reasons you should in fact do it this way.
setting up a port redirection with an iptables rule
This is probably better than directly having your node process listen on port 80, although I haven't seen this type of configuration used in production.
making express listen on port 80 within the app
This is functionally a poor choice because you don't get the benefits outlined in the linked answer above, however, from a strictly security standpoint, the key thing to remember is you must not run your node process as root, which would be a horrendous security problem. You must be root to bind to port 80 because that's a rule of unix, but you can and must change to a less-privileged user immediately after binding to that port.

Resources