ALH
ALH

Reputation: 85

HTTP server will not listen on port 8000

I'm new to node js and programming. I've installed node js and am trying to run the following code in a .js file:

JS

var http = require('http');
var server = http.createServer( function(request, response) {
    response.writeHead(200, {'Content-type': 'text/plain'});
    response.end('Hello World!');
    server.listen(8000);
    console.log('Server running on port 8000');
});

Next, I ran the command node helloworld.js on the terminal. However, when I tried to open

http://localhost:8000

on chrome, it says google chrome cannot connect to "localhost:8000". What is the problem?

Upvotes: 0

Views: 3025

Answers (1)

falsetru
falsetru

Reputation: 369064

It seems like you mis-typed an example code.

var http = require('http');
var server = http.createServer(function(request, response) {
    response.writeHead(200, {'Content-type': 'text/plain'});
    response.end('Hello World!');
}); // <-----------
server.listen(8000);
console.log('Server running on port 8000');

Upvotes: 2

Related Questions