Reputation: 9293
I'm on Chrome / Windows 7 and trying to make my first node.js steps using this tutorial:
https://www.w3schools.com/nodejs/nodejs_get_started.asp
So myfirst.js
is:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);
Going on http://localhost:8080/
I'm getting the error:
This site can’t be reached localhost refused to connect.
Try:
Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_REFUSED
Firewall is completely off.
Any help?
Upvotes: 0
Views: 4594
Reputation: 21
I had the same problem. I am using windows, so i resolved it by running Node.js and linking it to my script using the following command
.load C:\Users\Hercules\myfirst.js {Hercules is my user name}
My script looks like this.
# script name: myfirst.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);
I don't know if it is the correct way to do it, but it worked for me. I am new to node.js so please forgive me if my terminology is not 100% correct.
Upvotes: 0
Reputation: 1
I had the same problem but then I installed the pug inside the folder I was working on (basically the folder I named for my website) again and it worked. (idk why did I have to install the package inside the folder as it was installed already outside that folder)
Upvotes: 0
Reputation: 51
You need to initiate myfirst.js with node, on the CLI by typing the command for it to run
node myfirst.js
Upvotes: 1
Reputation: 1
server.listen(port, hostname, () => {
console.log(Server running at: http://${hostname}:${port}/
);
});
port=8080 hostname=127.0.0.1 or 192.168.1.x(your ip)
maybe 8080 is use by other software
Upvotes: 0
Reputation: 1
check your firewall you must edit in firewall system Node.js as public enter image description here
Upvotes: 0