O'Reilly logo

Instant Node.js Starter by Pedro Teixeira

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Quick start

Now that we have a working Node installation in place, we will see how easy it is to create and launch a new server with just a few keystrokes.

Step 1 – Creating a Hello World HTTP server

Let's then create our first Hello World server in Node.js. For that, create a file named hello_world_http_server.js with the following content:

var http = require('http');
var server = http.createServer();
server.on('request', function(req, res) {
    res.writeHead(200, {'content-type': 'text/plain'});
    res.write('Hello World!');
    res.end();
});

var port = 8080;
server.listen(port);
server.once('listening', function() {
    console.log('Hello World server listening on port %d', port);
});

Tip

Downloading the example code

You can download the example code files ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required