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

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>

Related

Unable to access localhost:3000 from other machine

I'm having a node.js application running on localhost:3000 and able to access it through my browser. I'm having the following configuration in my server.js file.
here port is 3000
app.listen(port, '0.0.0.0', function onStart(err) {
if (err) {
console.log(err);
}
console.info('==> 🌎 Listening on port %s. Open up http://localhost:%s/ in your browser.', port);
});
When I try to access my server using my laptop's IP by checking it through what's my ip on google e.g. ip is http://58.27.123.12:3000, it doesn't take me to the requested page. I'm running the application on Ubuntu 19.10.
I'm also able to ping my ip through other systems and it responds successfully. What I might be doing wrong? Thanks
if you want people to access on LAN give them your localIP with port number or if you want to provide access via internet check below links:-
https://localhost.run/
https://localtunnel.github.io/www/
ssh -R 80:localhost:3000 ssh.localhost.run
using this command solved the issue after installing localtunnel

nodejs app wont create server, says ERR_CONNECTION_TIMED_OUT

I'm trying to run a nodejs app on my VPS but for somereason I get a ERR_CONNECTION_TIMED_OUT error.
Here is my code:
const http = require('http');
const server = http.createServer((req, res) => {
res.end();
});
server.on('clientError', (err, socket) => {
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
});
server.listen(8000);
when i cd to my directory and do
node index.js
, it gives me no error, but going to the browser and doing: mysite.com:8000 gives me nothing. The connections times out
EDIT:
when i do curl: enomshop.tk:8000, I get some feedback. Its like i can access from the within the VPS but no access publicly
This is the way you should go about resolving such issues
Check if the process is running
Run ps aux | grep <process> to check if your process is running
Check if the port is being used
Run sudo netstat -plant | grep <port> to check if port is listening or not and is it your process
Check if the service is responding locally
Run telnet 127.0.0.1 <port> and make sure you get a response. If you don't get a response then there are few possible issues you can look into
The process is not started the server listening
The process is not using the correct port
There is a firewall that is blocking the port. There is this for firewall-cmd, and this for ufw
Check if the service is responding externally
You can do telnet <external ip> <port>, if it doesn't work then there few things you should check below
Make sure your server is binding to 0.0.0.0 or <private ip> and not 127.0.0.1
Make sure the VPS server you are hosting on, has your <port> enabled for communication. This is different for each service provider
If you follow these steps, it would solve most common network issues in such cases
I was facing the same issue. To resolve that, just open up / allow the port from network security group (VPS) or Firewall to worldwide access.
And check telnet mysite.com port, it worked for me. Kindly let me know if you still face any issue, I will try to help further.
You will need to open up the 8000 port on your VPS or redirect the traffic from any open port on VPS to 8000

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.

Node js : works on local, but not on my server

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.

I keep getting EADDRINUSE when deploying a simple node server

okay this is really strange , i am getting EADDRINUSE when implementing a simple server ,
i have changed the ports
looked for the already running node js process
tried using killall -9 node with root
looking for process hoarding the port through sudo netstat -alpn |grep node
var http= require('http');
var server=http.createServer(function(request,response){
response.end("kameha kameha....");
}).listen("162.x.x.38",8010);
process.stdin.resume();
process.on('uncaughtException',function(err){
console.log("closing due to uncaught exception:"+err.message+" "+err.data);
server.close();
process.exit(1);
});
process.on('exit',function(){
console.log("exiting the program");
server.close();
process.exit(0);
});
process.on('SIGINT', function(){
console.log("caught cntrl +c closing the program and performing cleanups");
server.close();
process.exit(0);
});
still no help , i am running centos 6.7 on a vps hosted by bluehost , so let me sum is down, there is no node process running , no prcocess is hoarding the port
strangely enough when i put my host as localhost it starts working but still i am unable to connect it via wget
wget localhost:8010
--2015-05-22 15:19:15-- http://localhost:8010/
Resolving localhost... ::1, 127.0.0.1
Connecting to localhost|::1|:8010... failed: Connection refused.
Connecting to localhost|127.0.0.1|:8010... failed: Connection refused.
when running as local host netstat gives the following :
sudo netstat -alp |grep node
unix 2 [ ACC ] STREAM LISTENING 453187 23439/node localhost
protocol as stated by netstat command is unix instead of tcp ,though i dont have much experience with linux so ....but its strange because above code is running fine in windows , but i dont know what is happening here ..help please !!
The parameters for http.listen() were backwards.
From the node.js docs:
server.listen(port[, hostname][, backlog][, callback])# Begin
accepting connections on the specified port and hostname. If the
hostname is omitted, the server will accept connections directed to
any IPv4 address (INADDR_ANY).
The correct call, from the docs link above, is as follows:
Simple, works on any address for the host
var server=http.createServer(function(request,response){
response.end("kameha kameha....");
}).listen(8010);
Bind to localhost only
var server=http.createServer(function(request,response){
response.end("kameha kameha....");
}).listen(8010, "127.0.0.1");
Bind to 1.2.3.4 is similar
var server=http.createServer(function(request,response){
response.end("kameha kameha....");
}).listen(8010, "1.2.3.4");

Resources