Rina
Rina

Reputation: 31

Node.js localhost:3000 refuses to connect

I am a total beginner of Node.js and I am unable to connect to localhost:3000

I use the following code in VS code, hit "node app.js" in terminal, and there is no error comes out in terminal at this point. However, as I try to access the localhost:3000, it keeps refusing: "ERR_CONNECTION_REFUSED" I searched on the internet for solutions and tried opening ports by creating an inbound rule on security settings, turned IIS on, used 127.0.0.1 instead, and still get refused. Does anyone have any idea how to solve this?

I am using Windows 10


const http = require('http');

var server = http.createServer(
    (request, response)=>{
        response.end('hello');
    }
);

server.listen(3000);

Upvotes: 2

Views: 6115

Answers (2)

DoneDeal0
DoneDeal0

Reputation: 6257

Here is how to fix it. Your probably try to launch your server on a used port.

// enter this command in your terminal
lsof -i:3000

// this will output the related PID (process ID). Here: 1382.
node      1382 name   21u  IPv6 blabla      0t0  TCP *:3000 (LISTEN)

// kill the PID in use
kill -9 1382

//relaunch your server
node app.js

Upvotes: 3

VeeGee
VeeGee

Reputation: 52

I ran it on my computer and that code works fine. I would try other ports to see if they work.

Upvotes: -1

Related Questions