Skip to content

Latest commit

 

History

History
71 lines (55 loc) · 3.01 KB

README.md

File metadata and controls

71 lines (55 loc) · 3.01 KB

0 - hello

This example is the canonical node "hello world" exercise, and forms an introduction to some of the basic concepts for working with node.

Install node

The first thing to do is install node 4.x, if you don't yet have it, you can get it from here.

Make an entry point

The next thing is to make our entry point - index.js - this is the file we will run using node, and will be where all of the code goes in this simple example.

Requiring modules

So, we have an empty index.js file, now we need to write some code that makes it do something - the first step in that is almost always by requireing some modules which provide functionality we can use.

In this case we require one of the many modules that are built-in to node, http, (see the rest here).

var http = require('http');

What require does is tells node to (synchronously) load the http module and return it to us, so we can store it in the variable http.

Later we'll go over how you do the opposite of requireing - exporting functionality for other files (or even packages) to use. You can also find a fairly thorough write-up of require and module.exports here.

Creating a server

Now we have the http module loaded, we can use it to make ourselves a basic web-server!

var http = require('http');

var server = http.createServer(function(request, response) {
    // TODO
});

The only thing we need to pass it is a callback, which will receive incoming requests and let us send them a response, but currently the callback we supplied doesn't do anything.

Responding to requests

Lets send the client a response whenever we get a request.

var http = require('http');

var server = http.createServer(function(request, response) {
    // Send back an OK status code (200) and tell them we're sending plain text
    response.writeHead(200, { 'Content-Type': 'text/plain' });

    // Then finalize the response with a string using response.end()
    response.end('Hello from node!');
});

Listening

The last thing our web server needs is to listen for requests.

var http = require('http');

var server = http.createServer(function(request, response) {
    // Send back an OK status code (200) and tell them we're sending plain text
    response.writeHead(200, { 'Content-Type': 'text/plain' });

    // Then finalize the response with a string using response.end()
    response.end('Hello from node!');
});

server.listen(3000, function() {
    console.log('Now listening on port 3000...');
});

We now have a fully functioning "Hello world" server!

Running node applications

To run the application you just have to enter (in a command prompt).

node index.js

It should then log out that it is listening (or any errors if something went wrong), and you can open http://localhost:3000/ to view the response it sends.