Hello, World

To introduce you to the world of writing JavaScript outside the browser, you are going to start with a classic Hello, World program. Create a new file named index.js within your chattrbox folder and type the following, which we will explain after you have entered it:

var http = require('http');

var server = http.createServer(function (req, res) {
  console.log('Responding to a request.');
  res.end('<h1>Hello, World</h1>');
});
server.listen(3000);

On the first line, you used Node’s built-in require function to access the http module included with Node. This module provides a number of tools for working with HTTP requests and responses, such as the http.createServer function.

http.createServer takes in one argument, ...

Get Front-End Web Development: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.