Reputation: 1554
I have a very weird thing happening while setting up and running a normal node js server.
When I run node server.js
, I get following error:
Server running at http://localhost:1337/
events.js:160
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND localhost
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
When I open http://localhost:1337/
in my browser, it says page does not exist
.
When I run sudo node server.js
, it runs fine without any error and I can access my site at https://localhost:1337
Here is my /etc/hosts file:
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
My server.js:
// Require what we need
var http = require("http");
// Build the server
var app = http.createServer(function(request, response {
response.writeHead(200, {
"Content-Type": "text/plain"
});
response.end("Hello world!\n");
});
// Start that server, baby
app.listen(1337, "localhost");
console.log("Server running at http://localhost:1337/");
Other Details
Following are my questions
Upvotes: 4
Views: 10559
Reputation: 19070
I have fixed this same error by editing the file /etc/hosts
Added the lines:
##
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
Upvotes: 4