Node js : works on local, but not on my server - node.js

When trying node on my webserver (hosted by some company), i realized that it doesn't work.
The issue i get is a timeout error.
On my local machine, the script works. On the server, the script doesn't work, but i confirmed that node works, with a 'hello world' program.
Here, to perform my test on the webserver, i use the simplest node program i can think of (beside 'hello world') :
Simple node program
var http = require('http');
var port = 8080;
console.log("*** Node script launched ***");
var server = http.createServer(function(req, res) {
console.log('Ok, server launched.');
res.writeHead(200);
res.end('Message from the server : ok!');
});
server.listen(port,'0.0.0.0',function(){
console.log((new Date())+' : OK. Server is listening.');
});
Edit : Corrected a typo in the program above. ==> Changed "Server.listen" into "server.listen". Thanks to #Num8er for noticing (unfortunately, this didn't solve the issue).
So after some research, i corrected one thing : specifying '0.0.0.0' as IP (before that, that part was left out). But it didn't solve my issue. And now, i can't find anything else that might be wrong (in the program. But i'm a newbie so...).
I suspect that the issue may come from my hoster, but i don't know how to pose a diagnostic on this situation.
Here is all the data i have :
Program output when launched on the webserver
*** Node script launched ***
Fri Aug 28 2015 01:45:00 GMT+0200 (CEST) : OK. Server is listening.
Output from browser (chrome)
This webpage is not available
ERR_TIMED_OUT
I have 2 questions :
Do you have an idea what the problem might be?
Do you know the steps i could take to be able to pose a diagnostic on this situation. Is there a way to tell if node is correctly listening? Is there a way to monitor if my client request gets to the server. If yes, is it blocked? Where is it blocked?
Thanks.
Loïc.

I think You're stopping application after You see the words:
"OK. Server is listening."
Code works without problem.
I believe that You're closing terminal or doing ctrl+c
Try to run the app using forever.
Install it using:
npm install -g forever
run Your app using:
forever start app.js
stop it using:
forever stopall
check status:
forever list
There is one more thing also.
If You using cloud services like C9.
so better change port line to:
var port = process.env.PORT || 8080;

Also, not sure about the webserver you're using, but the Port # might also be an issue.
I do know that on Heroku deployments I have to use something along the lines of:
var port = process.env.PORT || 8080;

This means that your application is fine but your port 8080 is not open in you server , Have you tried to navigate to http://serverip:8080 if it has ERR_TIMED_OUT problem , then the problem is in your port as I said.

Related

I can't run the node.js server in localhost

I am beginner in node.js programming. I did the simple program. I am running this program in localhost 8086.I am using Webstorm to execute this.This program works perfectly last two weeks. Suddenly it shows the error. I cannot able to run this program in localhost. It shows the error event in (port listen 8086). Can anyone solve this issue? Thanks in advance..
var express=require("express");
var app=express();
app.get("/abc",function(request,response)
{
response.send("I got a request");
console.log("I got a request");
});
app.listen(8086,function()
{
console.log("server running at port 8086");
});
Another application is already using that port. Check if you installed / started another software that might use the port 8086.
EARDRINUSE means that the port is already in use.
You had run another server use the same port like 8086 (in your case).
Maye you had run node app in other terminal in WebStorm. Please close it and run it again.
You can check PORT no. is available or not using by
netstat -tulnp | grep <port no>

502 Bad Gateway with nginx | Google App Engine | Node JS

I am hosting the web app on Google Cloud Platform with App Engine and I am using ExpressJS and MongoDB, which is hosted on mLab.
Everything worked well until 1/1/2017. I had vm:true before and now was forced to change the env to flex. Now I am getting 502 bad gateway error with nginx. App engine doesn't allow us to change the nginx config file.
I had tried the suggestion from this post: Google App Engine 502 (Bad Gateway) with NodeJS but still doesn't work.
For some reason, I have another app with exactly the same setting on app engine and it works perfectly.
Any suggestion will be greatly appreciated. Thank you.
app should always listen to port 8080, google forwards all request from 80 to 8080
https://cloud.google.com/appengine/docs/flexible/custom-runtimes/build#listen_to_port_8080
check out the logs for any deployment errors
$ gcloud app logs read
I have came across a similar issue with the code provided by this tutorial (https://cloud.google.com/nodejs/getting-started/authenticate-users)
And found there was a missing dependency. I fixed the missing dependency and the app is deployed and working fine.
Details into the issue: https://github.com/GoogleCloudPlatform/nodejs-getting-started/issues/106
I had the same problem with Express. What solved it for me was to not provide an IP address for the app.
So my old code would be:
var ip = "127.0.0.1";
var port = "8080";
var server = http.createServer(app);
server.listen(port, ip);
This would result in a 502 in app engine.
Removing the ip was the solution for me.
server.listen(port);
Set the host to 0.0.0.0
Port 8080 is set by default by the engine. In fact, you are not able to define the environment var PORT as it is reserved.
Run the next command (as mentioned by #sravan )
gcloud app logs read tail
and make sure it looks like this,
[Sun May 27 2018 10:32:44 GMT+0000 (UTC)] serving app on 0.0.0.0:8080
Cheers
Google App Engine uses an nginx front to load balance all requests for node.js apps. With nginx acting as a forward proxy, this error usually happens when the request the user is making in the browser is reaching nginx (you see the unstyled 502 bad gateway error page) but the nginx server is not able to correctly forward the request to your node app. There could be many issues why this is happening but here are some common ones:
By default, App Engine assumes your node app is running on 8080. nginx itself will run on 80 and forward the request to 8080. Check if your app's port number is 8080.
You app may have a hostname defined like a domain something.appspot.com or an IP 127.18.21.21 or the like. Remove any hostnames from your server.listen or config.json or vhost wherever. App Engine will take care of domains, IPs etc so you dont have to.
Your app may be crashing before its sending a response to nginx. Check the logs of both nginx AND your node app.
To check logs / find out what is going on use this guide https://cloud.google.com/appengine/docs/flexible/nodejs/debugging-an-instance#connecting_to_the_instance to SSH directly inside the VM behind app engine. There will be one docker process with nginx where you can see the nginx error log and one docker image with your node app to check your node app's error message.
I'm just wondering, based on the activity in this question and the timestamps, why hasn't Google updated its documentation to cover this issue!!! ???
Please take care of http also, while deploying, it should be http server not https
var server;
if (process.env.NODE_ENV == "dev") {
server = https.createServer(httpsOptions, app);
} else {
server = http.createServer(app);
}
A 502 is not necessarily an error with nginx itself, it can most often happen when the nginx proxy cannot talk to your app container (usually because your app failed to start). If you get a 502 after migrating to 'env: flex' this is most likely due to some code changes needed in your app as mentioned in Upgrading to the Latest App Engine Flexible Environment Release.
Checking your application logs for errors from NPM will also help to diagnose the exact reason for the failed startup.
Create a server and then check with a ternary condition if current environment is production or not, assign port '80' if current environment is development else assign process.env.NODE.ENV.
const app = require('express')();
const server = require('http').Server(app);
const port = process.env.NODE_ENV === 'production' ? process.env.PORT :'80';
server.listen(port, ()=> {
console.log('listening on port number *:' + server.address().port);
});
In my case, I had the same error due to google app engine update which trigged auto re-deployment of my React SPA to the google cloud vm. Then it leads to a build fail in the process because of incompatibility of runtime which is node 16.x.x. Compatible runtime was node 14.19.0. I had to specify node version in my package.json file and do the deployment again to fix 502 Bad Gateway error.
{
"engines": {
"node": "14.19.0"
}
}
Also refer:
https://cloud.google.com/appengine/docs/nodejs
https://cloud.google.com/appengine/docs/flexible/nodejs/runtime
Hope this helps with someone having this issue with React SPAs.

Can't find port 3000 after killing process

What happened was, I was fiddling with node, but when I killed node with ctrl+c, and started it up again i got an EADDRINUSE error:
EADDRINUSE, Address already in use
So I followed the advise given for killing :3000 using its PID: Kill localhost:3000 process from Windows command line
So, now when I run netstat -a -o, I can't find port 3000. Which makes sense, I killed it. But how do I get it up and running again, and set up so that port 3000 is listened to?!
Yeah, I've tried starting my server again, but nothing happens. Literally no response. It worked before.
Version: $ node -v
v4.2.2
Yes, I am a major n00b, let's get that out of the way. Thanks.
NODE JS CODE:
'use strict'
var port = 3000;
var express = require('express');
var app = express();
app.get('/', function(req,res){ res.send("I love treehosue"); });
app.listen(port, function(){ process.exit() console.log("server running on "+port); });
I had the same problem. The way I solved this issue was by simply erasing everything and starting anew. But instead of running (npm init --yes), I only ran npm init and manually filled the empty fields such as repo, etc. Hope this helps to everyone even though this question was posted in 2016.
The steps you have taken to kill the process are correct.
The problem you are experiencing is likely coming from unexpectedly terminating the application. The socket is missing a proper close operation and the OS blocks the port for a certain timeout.
From personal experience, on a linux system this can be somethink around 30sec.

Deploying a repository on an OpenShift node.js server

I'm using the Wercker Continuous Integration & Delivery Platform to test and deploy a BitBucket repository on a node.js OpenShift server. Wercker loads in the BitBucket repository, builds it, tests it in the node.js environment, and passes it without any issue. It's also checking the code with jsHint, and returns without any errors.
Wercker also indicates that the deployment passes without errors, as does OpenShift. The problem is that when I check the application URL provided to me by OpenShift, it results with a server error:
503 Service Temporarily Unavailable
The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.
In troubleshooting this, I restarted the server (I'm running the basic account, and I have that option) but that doesn't seem to resolve the issue. Neither Wercker or Openshift indicate that there is a problem, but for some reason, I'm simply unable to access that domain without error.
How can I fix this (with the most basic tier)?
This was the solution:
I installed the RHC client tools available on the OpenShift website, checked the application logs, and found that OpenShift was unable to find a server.js file in the root directory. So I renamed my app.js file to server.js, and in my package.json I changed the "start" value to server.js. Then I configured the code in server.js file to the OpenShift environment variables, and that did it!
The server.js now reads:
var http = require('http');
var ip = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1',
port = process.env.OPENSHIFT_NODEJS_PORT || '8080';
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(port, ip);
console.log('Server running at http://'+ip+':'+port+'/');
I'm now able to connect to the application URL and get the basic "Hello World" response.
(If at this point you're still unable to connect to your application, restart your server, and that should do the trick.)
I hope this helps someone else in the future.
Here's a helpful resource that I leaned on: https://gist.github.com/ryanj/5267357
Your app should be able to listen to the IP and port defined by Openshift's reverse proxy.
You need to change the port number and perhaps the IP in the server configuration.
Explained here: OpenShift node.js Error: listen EACCES

Node.js Deployment in openshift

I was trying to deploy a Node.js application to the openshift as in this link here
I understand this code
var http = require('http');
var server = http.createServer(function(req, res) {
res.writeHead(200);
res.end('Hello Http');
});
server.listen(3000);
and there is no issue running it locally
$ node server.js // saved as server.js
However, how does this work when I commit this application in openshift? This is very simple code. I have some downloaded code that is a chat application and client-server need to configure to listen on some port (I was using port number 3000 in my localhost).
It works on port number 3000 in localhost but how can I make it to work in Openshift?
You need to listen on port process.env.OPENSHIFT_NODEJS_PORT. So something like this should work:
server.listen(process.env.OPENSHIFT_NODEJS_PORT || 3000);
See here for example: Error: listen EACCES on Openshift app
Hey the issue with socket.io is that you have that npm package installed local but not in openshift (dependencies don't get pushed). For that you can login thru ssh (look for "Want to log in to your application?" in right menu in openshift control panel, follow instructions and use the ssh connection provided) then login with terminal o Putty, and go to:
cd app-root/repo
or
cd $OPENSHIFT_REPO_DIR
and then
npm install socket.io
I've used that to install mongoose and other dependencies without trouble. Also you can use
node server.js
from command line to run the site ;)

Resources