Node’s Hello World Example
Here is Node’s version of a “Hello World” example:
// server.js const http = require('http'); // 1 const server = http.createServer((req, res) => { // 2 res.end('Hello World\n'); // 3 }); // 4 server.listen(4242, () => { // 5 console.log('Server is running...'); // 6 });
This script represents a simple Web server. You don’t need to install anything to run this script. This is all Node’s built-in power.
To execute a script with Node, you just specify the location of that script as the argument for the node
command. The location can be absolute or relative (to where you are invoking the node command).
$ node relative/or/absolute/path/to/server.js
If you just provide a file name, the node
command will look for that file in the current working directory.
If the script has a running task (like a Web server listening for connections, for example), then Node will continue running.
Let’s decipher the simple web server example…
Line #1
uses the require
function. This is the first thing you need to learn about Node’s internal. The require
function is what you use to manage the dependencies of your programs. You can use require
to depend on a library, whether this library is a built-in one (like the http
one the example is using) or a 3rd-party installed one (like express
which you can get with NPM).
This web server example depends on the built-in http
module. It’s the module that has the feature of creating a web server. There are many other libraries that you can use to create a web server, but this one is built-in. You don’t need to install anything to use it, but you do need to require it.
In a Node’s REPL session, built-in modules (like http) are available immediately without needing to require them. This is not the case with executable scripts. You can’t use any dependencies (including built-in ones) without requiring them first. |
Line #2
creates a server constant by invoking the createServer
function on the http module. This function is one of many functions that are available under the http
module’s API. You can use it to create a web server object. It accepts an argument that is known as the Request Listener. The request listener is a simple function that Node will invoke every time there is a request to the web server.
This why this listener function receives the request object as an argument (named req
above but you can name it whatever you want). The other argument this listener function receives, named res
in the example, is a response object. It’s the other side for a request connection. We can use the res
object to write things back to the requester. It’s exactly what our simple web server is doing. It’s writing back — using the .end
method — the Hello World string.
The .end method can be used as a shortcut to write data and then end the request in one line.
|
The createServer
function only creates the server object. It does not activate it. To activate this web server, you need to invoke the listen
function on the created server (line #5).
The listen
function accepts many arguments, like what OS port and host to use for this server. The last argument for it is a function that will be invoked once the server is successfully running on the specified port. The example above just logs a message to indicate that the server is running successfully at that point.
Execute the server.js
script above using the node
command. While the server is running, if you go to a browser and ask for an http connection on localhost with the port that was used in the script (4242 in this case), you will see the Hello World string that this example had in its request listener function.