Node.js localhost:3000 refuses to connect - javascript

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);

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

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

Related

Node.js WebStorm remote debugging not working (without SSH tunnel)

I'm trying to debug on a live server running a Node.js webserver (Express, TypeScript).
I run the node server with NODE_ENV=development node --inspect=9229 --inspect-brk build/start.js, and it says it's waiting and listening (Debugger listening on ws://127.0.0.1:9229/a61485f2-aef8-4719-901d-4d5ad9d1e6cd).
I set up an SSH tunnel (using this method: https://gist.github.com/pajtai/081e0371e7366c79c0b9), and I set up an "Attach to Node.js/Chrome" debug configuration, with Host: localhost and Port: 9229. Then when I hit the debug button, it connects through the SSH tunnel, and the node.js process runs just fine, stopping and debugging at all my breakpoints.
However, when I don't set up the SSH tunnel, start the node server the same way, and set up an "Attach to Node.js/Chrome" profile with the host as the URL or IP of the server, WebStorm can never connect. There is a little red bubble over the "5: Debug" icon at the bottom saying it can't connect, and sometimes it says "Closed Explicitly". I have opened up port 9229 in the firewall settings for the server (I've tried TCP and UDP).
What am I doing wrong? Please help, anyone!
I'm using Mac OS X 10.15.3 and WebStorm 2019.3.1.
Node binds to localhost by default and thus can't be accessed from outside unless you set up the port forwarding (via ssh tunneling, for example).
You can try changing the command to node --inspect-brk=0.0.0.0:9229 build/start.js - does it help?
Note that Node.js discourages using remote debugging in this way: https://nodejs.org/en/docs/guides/debugging-getting-started/#enabling-remote-debugging-scenarios. Using SSH tunneling is recommended

live-server Error: listen EACCES 0.0.0.0:8080. even changing the ports not working? [duplicate]

I'm testing out an app (hopefully to run on heroku, but am having issues locally as well). It's giving me an EACCES error when it runs http.Server.listen() - but it only occurs on some ports.
So, locally I'm running:
joe#joebuntu:~$ node
> var h = require('http').createServer();
> h.listen(900);
Error: EACCES, Permission denied
at Server._doListen (net.js:1062:5)
at net.js:1033:14
at Object.lookup (dns.js:132:45)
at Server.listen (net.js:1027:20)
at [object Context]:1:3
at Interface.<anonymous> (repl.js:150:22)
at Interface.emit (events.js:42:17)
at Interface._onLine (readline.js:132:10)
at Interface._line (readline.js:387:8)
at Interface._ttyWrite (readline.js:564:14)
I don't have anything running on port 900 (or any of the other 20 ports I've tried), so this should work. The weird part is that it does work on some ports. For instance, port 3000 works perfectly.
What would cause this?
Update 1:
I figured out that on my local computer, the EACCES error is coming because I have to run node as root in order to bind to those certain ports. I don't know why this happens, but using sudo fixes it. However, this doesn't explain how I would fix it on Heroku. There is no way to run as root on Heroku, so how can I listen on port 80?
Running on your workstation
As a general rule, processes running without root privileges cannot bind to ports below 1024.
So try a higher port, or run with elevated privileges via sudo. You can downgrade privileges after you have bound to the low port using process.setgid and process.setuid.
Running on heroku
When running your apps on heroku you have to use the port as specified in the PORT environment variable.
See http://devcenter.heroku.com/articles/node-js
const server = require('http').createServer();
const port = process.env.PORT || 3000;
server.listen(port, () => console.log(`Listening on ${port}`));
#Windows
Another one reason - maybe your port has been excluded by some reasons.
So, try open CMD (command line) under admin rights and run :
net stop winnat
net start winnat
In my case it was enough.
Solution found here : https://medium.com/#Bartleby/ports-are-not-available-listen-tcp-0-0-0-0-3000-165892441b9d
Non-privileged user (not root) can't open a listening socket on ports below 1024.
Check this reference link:
Give Safe User Permission To Use Port 80
Remember, we do NOT want to run your applications as the root user,
but there is a hitch: your safe user does not have permission to use
the default HTTP port (80). You goal is to be able to publish a
website that visitors can use by navigating to an easy to use URL like
http://ip:port/
Unfortunately, unless you sign on as root, you’ll normally have to use
a URL like http://ip:port - where port number > 1024.
A lot of people get stuck here, but the solution is easy. There a few
options but this is the one I like. Type the following commands:
sudo apt-get install libcap2-bin
sudo setcap cap_net_bind_service=+ep `readlink -f \`which node\``
Now, when you tell a Node application that you want it to run on port
80, it will not complain.
On Windows System, restarting the service "Host Network Service", resolved the issue.
If you are using Windows. You should try restarting Windows NAT Driver service.
Open Command Prompt as Administrator and run
net stop winnat
then
net start winnat
That's it.
It's happening because I installed Nord VPN and it was auto staring with windows.
Another approach is to make port redirection:
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 900 -j REDIRECT --to-port 3000
And run your server on >1024 port:
require('http').createServer().listen(3000);
ps the same could be done for https(443) port by the way.
Spoiler alert: This answer may seems little funny.
I have spent more than 10 minutes to find out the root cause for this error in my system. I used this : PORT=2000; in my .env file.
Hope you already find it out. I had used a semicolon after declaring PORT number :'( I removed the extra sign and it started working.
I know this may not be answer for this question but hope it helps others who have faced same problem.
OMG!! In my case I was doing ....listen(ip, port) instead of ...listen(port, ip) and that was throwing up the error msg: Error: listen EACCES localhost
I was using port numbers >= 3000 and even tried with admin access. Nothing worked out. Then with a closer relook, I noticed the issue. Changed it to ...listen(port, ip) and everything started working fine!!
Just calling this out in case if its useful to someone else...
I had a similar problem that it was denying to run on port 8080, but also any other.
Turns out, it was because the env.local file it read contained comments after the variable names like:
PORT=8080 # The port the server runs at
And it interpreted it like that, trying to use port "8080 # The port the server runs at", which is obviously an invalid port (-1).
Removing the comments entirely solved it.
Using Windows 10 and Git Bash by the way.
I know it's not exactly the problem described here, but it might help someone out there. I landed on this question searching for the problem for my answer, so... maybe?
It means node is not able to listen on defined port. Change it to something like 1234 or 2000 or 3000 and restart your server.
restart was not enough! The only way to solve the problem is by the following:
You have to kill the service which run at that port.
at cmd, run as admin, then type :
netstat -aon | find /i "listening"
Then, you will get a list with the active service, search for the port that is running at 4200n and use the process id which is the last column to kill it by
: taskkill /F /PID 2652
I got this error on my mac because it ran the apache server by default using the same port as the one used by the node server which in my case was the port 80. All I had to do is stop it with sudo apachectl stop
Hope this helps someone.
Remember if you use sudo to bind to port 80 and are using the env variables PORT & NODE_ENV you must reexport those vars as you are now under root profile and not your user profile. So, to get this to work on my Mac i did the following:
sudo su
export NODE_ENV=production
export PORT=80
docpad run
I got this error on my mac too. I use npm run dev to run my Nodejs app in Windows and it works fine. But I got this error on my mac - error given was: Error: bind EACCES null:80.
One way to solve this is to run it with root access. You may use sudo npm run dev and will need you to put in your password.
It is generally preferable to serve your application on a non privileged port, such as 3000, which will work without root permissions.
reference: Node.js EACCES error when listening on http 80 port (permission denied)
this happens if the port you are trying to locally host on is portfowarded
Try authbind:
http://manpages.ubuntu.com/manpages/hardy/man1/authbind.1.html
After installing, you can add a file with the name of the port number you want to use in the following folder: /etc/authbind/byport/
Give it 500 permissions using chmod and change the ownership to the user you want to run the program under.
After that, do "authbind node ..." as that user in your project.
My error is resolved using (On Windows)
app.set('PORT', 4000 || process.env.PORT);
app.listen(app.get('PORT'), <IP4 address> , () => {
console.log("Server is running at " + app.get('PORT'));
});
Allow the NodeJS app to access the network in Windows Firewall.
My error got resolved just by changing port number in server.js
Specially in this line
const port = process.env.PORT || 8085;
I changed my port number to 8085 from 8080.
Hope it helps.
For me this issue affected all hosts and all ports on Windows in PowerShell.
Disabling Network Interfaces fixed the issue.
I had WiFi and an Ethernet connection and disabling the Ethernet Interface fixed this issue.
Open "Network Connections" to view your interfaces. Right-click and select "Disable".
This means the port is used somewhere else. so, you need to try another one or stop using the old port.
I tried every answer given above, but nothing works out, then I figured out that I forget to add const before declaring the variable in the .env file.
Before:
PORT = 5000;
HOST = "127.0.0.1";
After:
const PORT = 5000;
const HOST = "127.0.0.1";
So the possible reason for this would be related to typo in environment variables names or else not installed dotenv package .
So if there is any other error apart from typo and dotenv npm package ,then you must try these solutions which are given below:
First solution
for Windows only
Open cmd as run as administrator and then write two commands
net stop winnat
net start winnat
hope this may solve the problem ...
Second solution
for windows only
make sure to see that in your environment variable that there is no semicolon(;) at the end of the variable and there is no colon (:) after the variable name.
for example I was working on my project in which my env variables were not working so the structure for my .env file was like PORT:5000; CONNECTION_URL:MongoDbString.<password>/<dbname>; so it was giving me this error
Error: listen EACCES: permission denied 5000;
at Server.setupListenHandle [as _listen2] (node:net:1313:21)
at listenInCluster (node:net:1378:12)
at Server.listen (node:net:1476:5)
at Function.listen (E:\MERN REACT\mern_memories\server\node_modules\express\lib\application.js:618:24)
at file:///E:/MERN%20REACT/mern_memories/server/index.js:29:9
at processTicksAndRejections (node:internal/process/task_queues:96:5)
Emitted 'error' event on Server instance at:
at emitErrorNT (node:net:1357:8)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'EACCES',
errno: -4092,
syscall: 'listen',
address: '5000;',
port: -1
}
[nodemon] app crashed - waiting for file changes before starting...
So i did some changes in my env file this time i removed the colon(:) and replaced it with equal(=) and removed semi colon at the end so my .env file was looking like this
PORT = 5000
CONNECTION_URL = MongoDbString.<password>/<dbname>
After changing these thing my server was running on the port 5000 without any warning and issues
Hope this may works...
#code #developers #mernstack #nodejs #react #windows #hostservicenetwork #http #permission-denied #EACCES:-4092
After trying many different ways, re-installing IIS on my windows solved the problem.
The same issue happened to me.
You need to check out your server.js file where you are setting your listening port. Change port number wisely in all places, and it will solve your issue hopefully.
For me, it was just an error in the .env file. I deleted the comma at the end of each line and it was solved.
Before:
HOST=127.0.0.1,
After:
HOST=127.0.0.1
Error: listen EACCES: permission denied 3000;
i add "PORT = 3000;" while "PORT = 3000" .
just semicolon";" give error
remove semicolon and project run successfully
I had a similar problem that it was denying to run on port 5000,
Turns out, it was because the env.local file contained comma(';') after variable names like:
PORT= 5000;
And it interpreted it like that, trying to use port "5000;", which is obviously an invalid port (-1). Removing the ';' entirely solved it.
I know it's not exactly the problem described here, but it might help someone out there. I landed on this question searching for the problem for my answer, so... maybe?
This worked perfectly fine for me, set your port at the bottom of the page with this code instead
let port = process.env.PORT;
if (port == null || port == "") {
port = 3000;
}
app.listen(port, function() {
console.log('app started successfully')
});
Some times it is because of bad configuration the dot.env like: require("dotenv").config without () in your app middle ware or may be you write your port number with wrong syntax like instead of = you write : or add some other symbols in port number.

Running node app forever with sudo leads to errors [duplicate]

I'm testing out an app (hopefully to run on heroku, but am having issues locally as well). It's giving me an EACCES error when it runs http.Server.listen() - but it only occurs on some ports.
So, locally I'm running:
joe#joebuntu:~$ node
> var h = require('http').createServer();
> h.listen(900);
Error: EACCES, Permission denied
at Server._doListen (net.js:1062:5)
at net.js:1033:14
at Object.lookup (dns.js:132:45)
at Server.listen (net.js:1027:20)
at [object Context]:1:3
at Interface.<anonymous> (repl.js:150:22)
at Interface.emit (events.js:42:17)
at Interface._onLine (readline.js:132:10)
at Interface._line (readline.js:387:8)
at Interface._ttyWrite (readline.js:564:14)
I don't have anything running on port 900 (or any of the other 20 ports I've tried), so this should work. The weird part is that it does work on some ports. For instance, port 3000 works perfectly.
What would cause this?
Update 1:
I figured out that on my local computer, the EACCES error is coming because I have to run node as root in order to bind to those certain ports. I don't know why this happens, but using sudo fixes it. However, this doesn't explain how I would fix it on Heroku. There is no way to run as root on Heroku, so how can I listen on port 80?
Running on your workstation
As a general rule, processes running without root privileges cannot bind to ports below 1024.
So try a higher port, or run with elevated privileges via sudo. You can downgrade privileges after you have bound to the low port using process.setgid and process.setuid.
Running on heroku
When running your apps on heroku you have to use the port as specified in the PORT environment variable.
See http://devcenter.heroku.com/articles/node-js
const server = require('http').createServer();
const port = process.env.PORT || 3000;
server.listen(port, () => console.log(`Listening on ${port}`));
#Windows
Another one reason - maybe your port has been excluded by some reasons.
So, try open CMD (command line) under admin rights and run :
net stop winnat
net start winnat
In my case it was enough.
Solution found here : https://medium.com/#Bartleby/ports-are-not-available-listen-tcp-0-0-0-0-3000-165892441b9d
Non-privileged user (not root) can't open a listening socket on ports below 1024.
Check this reference link:
Give Safe User Permission To Use Port 80
Remember, we do NOT want to run your applications as the root user,
but there is a hitch: your safe user does not have permission to use
the default HTTP port (80). You goal is to be able to publish a
website that visitors can use by navigating to an easy to use URL like
http://ip:port/
Unfortunately, unless you sign on as root, you’ll normally have to use
a URL like http://ip:port - where port number > 1024.
A lot of people get stuck here, but the solution is easy. There a few
options but this is the one I like. Type the following commands:
sudo apt-get install libcap2-bin
sudo setcap cap_net_bind_service=+ep `readlink -f \`which node\``
Now, when you tell a Node application that you want it to run on port
80, it will not complain.
On Windows System, restarting the service "Host Network Service", resolved the issue.
If you are using Windows. You should try restarting Windows NAT Driver service.
Open Command Prompt as Administrator and run
net stop winnat
then
net start winnat
That's it.
It's happening because I installed Nord VPN and it was auto staring with windows.
Another approach is to make port redirection:
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 900 -j REDIRECT --to-port 3000
And run your server on >1024 port:
require('http').createServer().listen(3000);
ps the same could be done for https(443) port by the way.
Spoiler alert: This answer may seems little funny.
I have spent more than 10 minutes to find out the root cause for this error in my system. I used this : PORT=2000; in my .env file.
Hope you already find it out. I had used a semicolon after declaring PORT number :'( I removed the extra sign and it started working.
I know this may not be answer for this question but hope it helps others who have faced same problem.
OMG!! In my case I was doing ....listen(ip, port) instead of ...listen(port, ip) and that was throwing up the error msg: Error: listen EACCES localhost
I was using port numbers >= 3000 and even tried with admin access. Nothing worked out. Then with a closer relook, I noticed the issue. Changed it to ...listen(port, ip) and everything started working fine!!
Just calling this out in case if its useful to someone else...
I had a similar problem that it was denying to run on port 8080, but also any other.
Turns out, it was because the env.local file it read contained comments after the variable names like:
PORT=8080 # The port the server runs at
And it interpreted it like that, trying to use port "8080 # The port the server runs at", which is obviously an invalid port (-1).
Removing the comments entirely solved it.
Using Windows 10 and Git Bash by the way.
I know it's not exactly the problem described here, but it might help someone out there. I landed on this question searching for the problem for my answer, so... maybe?
It means node is not able to listen on defined port. Change it to something like 1234 or 2000 or 3000 and restart your server.
restart was not enough! The only way to solve the problem is by the following:
You have to kill the service which run at that port.
at cmd, run as admin, then type :
netstat -aon | find /i "listening"
Then, you will get a list with the active service, search for the port that is running at 4200n and use the process id which is the last column to kill it by
: taskkill /F /PID 2652
I got this error on my mac because it ran the apache server by default using the same port as the one used by the node server which in my case was the port 80. All I had to do is stop it with sudo apachectl stop
Hope this helps someone.
Remember if you use sudo to bind to port 80 and are using the env variables PORT & NODE_ENV you must reexport those vars as you are now under root profile and not your user profile. So, to get this to work on my Mac i did the following:
sudo su
export NODE_ENV=production
export PORT=80
docpad run
I got this error on my mac too. I use npm run dev to run my Nodejs app in Windows and it works fine. But I got this error on my mac - error given was: Error: bind EACCES null:80.
One way to solve this is to run it with root access. You may use sudo npm run dev and will need you to put in your password.
It is generally preferable to serve your application on a non privileged port, such as 3000, which will work without root permissions.
reference: Node.js EACCES error when listening on http 80 port (permission denied)
this happens if the port you are trying to locally host on is portfowarded
Try authbind:
http://manpages.ubuntu.com/manpages/hardy/man1/authbind.1.html
After installing, you can add a file with the name of the port number you want to use in the following folder: /etc/authbind/byport/
Give it 500 permissions using chmod and change the ownership to the user you want to run the program under.
After that, do "authbind node ..." as that user in your project.
My error is resolved using (On Windows)
app.set('PORT', 4000 || process.env.PORT);
app.listen(app.get('PORT'), <IP4 address> , () => {
console.log("Server is running at " + app.get('PORT'));
});
Allow the NodeJS app to access the network in Windows Firewall.
My error got resolved just by changing port number in server.js
Specially in this line
const port = process.env.PORT || 8085;
I changed my port number to 8085 from 8080.
Hope it helps.
For me this issue affected all hosts and all ports on Windows in PowerShell.
Disabling Network Interfaces fixed the issue.
I had WiFi and an Ethernet connection and disabling the Ethernet Interface fixed this issue.
Open "Network Connections" to view your interfaces. Right-click and select "Disable".
This means the port is used somewhere else. so, you need to try another one or stop using the old port.
I tried every answer given above, but nothing works out, then I figured out that I forget to add const before declaring the variable in the .env file.
Before:
PORT = 5000;
HOST = "127.0.0.1";
After:
const PORT = 5000;
const HOST = "127.0.0.1";
So the possible reason for this would be related to typo in environment variables names or else not installed dotenv package .
So if there is any other error apart from typo and dotenv npm package ,then you must try these solutions which are given below:
First solution
for Windows only
Open cmd as run as administrator and then write two commands
net stop winnat
net start winnat
hope this may solve the problem ...
Second solution
for windows only
make sure to see that in your environment variable that there is no semicolon(;) at the end of the variable and there is no colon (:) after the variable name.
for example I was working on my project in which my env variables were not working so the structure for my .env file was like PORT:5000; CONNECTION_URL:MongoDbString.<password>/<dbname>; so it was giving me this error
Error: listen EACCES: permission denied 5000;
at Server.setupListenHandle [as _listen2] (node:net:1313:21)
at listenInCluster (node:net:1378:12)
at Server.listen (node:net:1476:5)
at Function.listen (E:\MERN REACT\mern_memories\server\node_modules\express\lib\application.js:618:24)
at file:///E:/MERN%20REACT/mern_memories/server/index.js:29:9
at processTicksAndRejections (node:internal/process/task_queues:96:5)
Emitted 'error' event on Server instance at:
at emitErrorNT (node:net:1357:8)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'EACCES',
errno: -4092,
syscall: 'listen',
address: '5000;',
port: -1
}
[nodemon] app crashed - waiting for file changes before starting...
So i did some changes in my env file this time i removed the colon(:) and replaced it with equal(=) and removed semi colon at the end so my .env file was looking like this
PORT = 5000
CONNECTION_URL = MongoDbString.<password>/<dbname>
After changing these thing my server was running on the port 5000 without any warning and issues
Hope this may works...
#code #developers #mernstack #nodejs #react #windows #hostservicenetwork #http #permission-denied #EACCES:-4092
After trying many different ways, re-installing IIS on my windows solved the problem.
The same issue happened to me.
You need to check out your server.js file where you are setting your listening port. Change port number wisely in all places, and it will solve your issue hopefully.
For me, it was just an error in the .env file. I deleted the comma at the end of each line and it was solved.
Before:
HOST=127.0.0.1,
After:
HOST=127.0.0.1
Error: listen EACCES: permission denied 3000;
i add "PORT = 3000;" while "PORT = 3000" .
just semicolon";" give error
remove semicolon and project run successfully
I had a similar problem that it was denying to run on port 5000,
Turns out, it was because the env.local file contained comma(';') after variable names like:
PORT= 5000;
And it interpreted it like that, trying to use port "5000;", which is obviously an invalid port (-1). Removing the ';' entirely solved it.
I know it's not exactly the problem described here, but it might help someone out there. I landed on this question searching for the problem for my answer, so... maybe?
This worked perfectly fine for me, set your port at the bottom of the page with this code instead
let port = process.env.PORT;
if (port == null || port == "") {
port = 3000;
}
app.listen(port, function() {
console.log('app started successfully')
});
Some times it is because of bad configuration the dot.env like: require("dotenv").config without () in your app middle ware or may be you write your port number with wrong syntax like instead of = you write : or add some other symbols in port number.

Meteor deployed on server, but browser says site can't be reached

So I've deployed my meteor app, and have it up and running on an instance.
I've used the following environment variables:
MONGO_URL='mongodb://localhost:27017/meteor'
ROOT_URL='http://<my static ip>'
PORT=3000
And I run the program using the following command:
node bundle/main.js
It prints my "Meteor is starting up" that is printed using the console.log command, and then doesn't error out, but when I navigate to http://< my static ip >:3000 in a browser, I get an ERR_CONNECTION_REFUSED result.
My open mongod terminal says it's connecting fine to the MongoDB database.
Does anyone have any ideas on how to start debugging this issue?
Thanks.
In server you don't need to run meteor application on port 3000. You can run it on port 80 if the port is not being used by any other program.
If you are using port 80 make sure port 80 is opened by the network security rules.
If you are using port 3000 or any other port you will have to make sure that port is opened by the network security rules as above. Additionally you will have to mention the IP in your url, like http://<your_ip>:<port>

How could others, on a local network, access my NodeJS app while it's running on my machine?

I have a pretty straight-forward question. I made a web game with NodeJS, and I can successfully play it by myself with multiple browser windows open side-by-side; however, I'd like to know if it's possible for other local machines to be able to access and play the game with me too.
I naively tried using this url: my-ip-address:8000 and it won't work.
Your node.js server is running on a port determined at the end of the script usually. Sometimes 3000. but can be anything. The correct way for others to access is as you say...
http://your.network.ip.address:port/
e.g.
http://192.168.0.3:3000
Check you have the correct port - and the IP address on the network - not the internet IP.
Otherwise, maybe the ports are being blocked by your router. Try using 8080 or 80 to get around this - otherwise re-configure your router.
If you are using a router then:
Replace server.listen(yourport, 'localhost'); with server.listen(yourport, 'your ipv4 address');
in my machine it is
server.listen(3000, '192.168.0.3');
Make sure yourport is forwarded to your ipv4 address.
On Windows Firewall, tick all on Node.js:Server-side JavaScript.
I had the same question and solved the problem. In my case, the Windows Firewall (not the router) was blocking the V8 machine I/O on the hosting machine.
Go to windows button
Search "Firewall"
Choose "Allow programs to communicate through Firewall"
Click Change Setup
Tick all of "Evented I/O for V8 Javascript" OR "Node.js: Server-side Javascript"
My guess is that "Evented I/O for V8 Javascript" is the I/O process that node.js communicates to outside world and we need to free it before it can send packets outside of the local computer. After enabling this program to communicate over Windows firewall, I could use any port numbers to listen.
One tip that nobody has mentioned yet is to remember to host the app on the LAN-accessible address 0.0.0.0 instead of the default localhost. Firewalls on Mac and Linux are less strict about this address compared to the default localhost address (127.0.0.1).
For example,
gatsby develop --host 0.0.0.0
yarn start --host 0.0.0.0
npm start --host 0.0.0.0
You can then access the address to connect to by entering ifconfig or ipconfig in the terminal. Then try one of the IP addresses on the left that does not end in .255 or .0
Faced similar issue with my Angular Node Server(v6.10.3) which set up in WIndows 10.
http://localhost:4201 worked fine in localhost. But http://{ipaddress}:4201 not working in other machines in local network.
For this I updated the ng serve like this
//Older ng serve in windows command Prompt
ng serve --host localhost --port 4201
//Updated ng serve
//ng serve --host {ipaddress} --port {portno}
ng serve --host 192.168.1.104 --port 4201
After doing this modification able to access my application in other machines in network bt calling this url
http://192.168.1.104:4201
//http://{ipaddress}:4201
The port is probably blocked by your local firewall or router. Hard to tell without details.
But there is a simple solution for which you don't have to mess with firewall rules, run node as a privileded process to serve on port 80, etc...
Check out Localtunnel. Its a great Ruby script/service, which allows you to make any local port available on the internet within seconds. It's certainly not useful for a production setup, but to try out a game with colleagues, it should work just fine!
const express = require('express');
var app = express();
app.listen(Port Number, "Your IP Address");
// e.g.
app.listen(3000, "192.183.190.3");
You can get your IP Address by typing ipconfig in cmd if your Windows user else you can use ifconfig.
After trying many solution and lot of research I did to the following to make sure my localhost is accessible from other machine in same network. I didn't start my server with IPAddress as parameter to listen method as suggested by others in this question. I did the following to make sure my local node js server is accessible from other machine on same local network. My node server is running in Windows 10 machine.
Open "Windows Defender Firewall with Advanced Security"
Select "Inbound Rules" in the left pane.
In the list of available rules, "Node.js Server-side Javascript" has "Block the connection" radio checked. Change this to "Allow the connection".
Please see the attached screenshot:
After these changes, I am able to access my localhost using http://IPAddress:Port/
Thanks.
And Don't Forget To Change in Index.html Following Code :
<script src="http://192.168.1.4:8000/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
var socket = io.connect('http://192.168.1.4:8000');
Good luck!
This worked for me and I think this is the most basic solution which involves the least setup possible:
With your PC and other device connected to the same network , open cmd from your PC which you plan to set up as a server, and hit ipconfig to get your ip address.
Note this ip address. It should be something like "192.168.1.2" which is the value to the right of IPv4 Address field as shown in below format:
Wireless LAN adapter Wi-Fi:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : ffff::ffff:ffff:ffff:ffad%14
IPv4 Address. . . . . . . . . . . : 192.168.1.2
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Start your node server like this : npm start <IP obtained in step 1:3000> e.g. npm start 192.168.1.2:3000
Open browser of your other device and hit the url: <your_ip:3000> i.e. 192.168.1.2:3000 and you will see your website.
put this codes in your server.js :
app.set('port', (80))
app.listen(app.get('port'), () => {
console.log('Node app is running on port', app.get('port'))
})
after that if you can't access app on network disable firewall like this :
ngrok allows you to expose a port on the internet with custom forwarding refs:
$ npx ngrok http 8000
First, check your ipv4 address. In my case my ipv4 address is 192.168.44.112. If you don't know your ipv4 address, run this command on cmd.
ipconfig
Follow this code...
const express = require('express');
const app = express();
const port = process.env.port || 8000
app.get('/', (req, res) => {
res.send("Hello Network!")
});
app.listen(port, '192.168.77.112', ()=>{
console.log(`Listening port on ${port}`)
});
In Ubuntu you can fix this by allowing a specific port or port range:
sudo ufw allow PORT-NUMBER/tcp
example:
sudo ufw allow 3000/tcp
or a range:
sudo ufw allow 3000:3001/tcp
var http = require('http');
http.createServer(function (req, res) {
}).listen(80, '127.0.0.1');
console.log('Server running at http://127.0.0.1:80/');
By default node will run on every IP address exposed by the host on which it runs. You don't need to do anything special. You already knew the server runs on a particular port. You can prove this, by using that IP address on a browser on that machine:
http://my-ip-address:port
If that didn't work, you might have your IP address wrong.
I had this problem. The solution was to allow node.js through the server's firewall.

Categories