Advertisement
Scroll to top

Event-driven programming can be overwhelming for beginners, which can make Node.js difficult to get started with. But don't let that discourage you. In this article, I will teach you some of the basics of Node.js and explain why it has become so popular.

Introduction

To start using Node.js, you must first understand the differences between Node.js and traditional server-side scripting environments like PHP, Python, or Ruby.

Asynchronous Programming

Chances are good that you are familiar with asynchronous programming; it is, after all, the "A" in AJAX. Every function in Node is asynchronous. Therefore, everything that would normally block the thread is instead executed in the background using promises. This is the most important thing to remember about Node. For example, if you are reading a file on the file system, you will use an asynchronous function.

If you are using certain versions of Node functions, you use callbacks, which were designed before promises existed. Most Node functions have promise equivalents now, so it's a good idea to convert the callback functions used here to their promise-based equivalents and compare the syntax.

You Are Doing Everything!

With Node, you have to do a lot yourself. For example, the HTTP module is minimal and unopinionated. This can be overwhelming for new users, but the payoff is a high-performing web app (although a large part of JavaScript's performance is because of V8's optimized engine). One script handles all communication with the clients. This considerably reduces the number of resources used by the application. For example, here is the code for a simple Node.js application:

1
const i, a, b, c, max;
2
3
max = 1000000000;
4
5
var d = Date.now();
6
7
for (i = 0; i < max; i++) {
8
    a = 1234 + 5678 + i;
9
    b = 1234 * 5678 + i;
10
    c = 1234 / 2 + i;
11
}
12
13
console.log(Date.now() - d);

And here is the equivalent written in PHP:

1
$a = null;
2
$b = null;
3
$c = null;
4
$i = null;
5
$max = 1000000000;
6
7
$start = microtime(true);
8
9
for ($i = 0; $i < $max; $i++) {
10
    $a = 1234 + 5678 + $i;
11
    $b = 1234 * 5678 + $i;
12
    $c = 1234 / 2 + $i;
13
}
14
15
var_dump(microtime(true) - $start);

Now let's look at the benchmark numbers. The following table lists the response times, in milliseconds, for these two simple applications:

Number of iterations Node.js PHP
100 2.00 0.14
10'000 3.00 10.53
1'000'000 15.00 1119.24
10'000'000 143.00 10621.46
1'000'000'000 11118.00 1036272.19

I executed the two apps from the command line so that no server would delay the apps' execution. I ran each test ten times and averaged the results. PHP is notably faster with a smaller number of iterations, but that advantage quickly dissolves as the number of iterations increases. When all is said and done, PHP is 93% slower than Node.js!

Node.js is fast, but you will need to learn a few things in order to use it properly.

Modules

Node.js uses a module architecture to simplify the creation of complex applications. Modules are akin to libraries in C or units in Pascal. Each module is private by default but can export various values (for example, functions) to other modules, which can import them. For example, the http module contains functions specific to HTTP.

Node.js provides a few core modules out of the box to help you access files on the file system, create HTTP and TCP/UDP servers, and perform other useful functions. There are two different methods of using modules, CommonJS (CJS) and ECMAScript Modules (ESM). CommonJS is most commonly used in legacy Node programs, whereas ECMAScript Modules are used in browsers and more modern Node programs.

CommonJS

Including a module is easy; simply call the require() function, like this:

1
const http = require('http');

The require() function returns the reference to the specified module. In the case of this code, a reference to the http module is stored in the http variable.

In the above code, we passed the name of a module to the require() function. This causes Node to search for a node_modules folder in our application's directory and search for the http module in that folder. If Node does not find the node_modules folder (or the http module within it), it then looks through the global module cache. You can also specify an actual file by passing a relative or absolute path, like so:

1
const myModule = require('./myModule.js');

To export a value that can be required, use the exports object and populate its properties and methods with the pieces of code that you want to expose. Consider the following module as an example:

1
exports.area = function (r) {
2
  return Math.PI * r ** 2;
3
};
4
5
exports.circumference = function (r) {
6
  return 2 * Math.PI * r;
7
};

This code creates a PI variable that can only be accessed by code contained within the module; it is not accessible outside of the module. Next, two functions are created on the exports object. These functions are accessible outside of the module because they are defined on the exports object. As a result, PI is completely protected from outside interference. Therefore, you can rest assured that area() and circumference() will always behave as they should (as long as a value is supplied for the r parameter).

ECMAScript Modules

To include a module, you use the import keyword:

1
// file

2
import myModule from "./myModule.js"
3
// Node builtin/package

4
import http from "http"

In the browser, ESM can only search using relative file paths, like the above. However, in Node, you can pass a path without ./ to search for a Node built-in or NPM package instead. Another way to import Node built-ins and packages is to use the node: prefix.

However, the previous approach only works with default exports. If you are using named exports (more on those later), you will need to use a slightly different syntax.

1
import { exportOne, exportTwo } from "./myModule.js"

To export something, use the export or export default keyword. Export is for named exports, and export default is for default exports. Named exports allow you to export different things and only use one of them in a different module using the import syntax above. Default exports make it easier if you are only exporting a single thing.

1
// Named exports
2
export function exportOne() {
3
    ...
4
}
5
export function exportTwo() {
6
    ...
7
}
8
// Default exports
9
export default function defaultFunction() {
10
    ...
11
}

For the rest of this tutorial, we will be using ESM.

Global Scope

Node is a JavaScript environment running in Google's V8 JavaScript engine. As such, we should follow the best practices that we use for client-side development. For example, we should avoid putting anything into the global scope. That, however, is not always possible. The global scope in Node is GLOBAL (as opposed to window in the browser), and you can easily create a global variable of a function by omitting the var keyword, like this:

1
globalVariable = 1;
2
globalFunction = function () { ... };

Once again, globals should be avoided whenever possible. So be careful, and remember to use var when declaring a variable.

Installation

Naturally, we need to install Node before we can write and execute an app. Installation is straightforward if you use Windows or macOS; the nodejs.org website offers installers for those operating systems. For Linux, use any package manager. For example, if you are using a distro that supports apt, open up your terminal and type:

1
sudo apt-get update
2
sudo apt-get install node

or:

1
sudo aptitude update
2
sudo aptitude install node

Node.js is in sid repositories; you may need to add them to your sources list:

1
sudo echo deb https://ftp.us.debian.org/debian/ sid main > /etc/apt/sources.list.d/sid.list

But be aware that installing sid packages on older systems may break your system. Be careful, and remove /etc/apt/sources.list.d/sid.list after you finish installing Node.

Installing New Modules

Node.js has a package manager, called Node Package Manager (NPM). It is automatically installed with Node.js, and you use NPM to install new modules. To install a module, open your terminal/command line, navigate to the desired folder, and execute the following command:

1
npm install module_name

It doesn't matter what OS you have; the above command will install the module you specify in place of module_name.

The Hello World App

Naturally, our first Node.js script will print the text 'Hello World!' to the console. Create a file called hello.js, and type the following code:

1
console.log('Hello World!');

Now let's execute the script. Open the terminal/command line, navigate to the folder that contains hello.js, and execute the following command:

1
node hello.js

You should see 'Hello World!' displayed in the console.

HTTP Server

Let's move on to a more advanced application; it's not as complicated as you may think. Let's start with the following code. Read the comments and then the explanation below:

1
// Include http module.

2
import { createServer } from "http"
3
4
// Create the server. Function passed as parameter is called on every request made.

5
// request variable holds all request parameters

6
// response variable allows you to do anything with response sent to the client.

7
createServer(function (request, response) {
8
    // Attach listener on end event.

9
	// This event is called when client sent all data and is waiting for response.

10
	request.on("end", function () {
11
		// Write headers to the response.

12
		// 200 is HTTP status code (this one means success)

13
		// Second parameter holds header fields in object

14
		// We are sending plain text, so Content-Type should be text/plain

15
		response.writeHead(200, {
16
			'Content-Type': 'text/plain'
17
		});
18
		// Send data and end response.

19
		response.end('Hello HTTP!');
20
	});
21
// Listen on the 8080 port.

22
}).listen(8080);

This code is very simple. You can send more data to the client by using the response.write() method, but you have to call it before calling response.end(). Save this code as http.js and type the following command into your console:

1
node http.js

Open up your browser and navigate to http://localhost:8080. You should see the text Hello HTTP! on the page.

Handling URL Parameters

As I mentioned earlier, we have to do everything ourselves in Node, including parsing request arguments. This is, however, fairly simple. Take a look at the following code:

1
// Include createServer

2
import { createServer} from "http"
3
// And url module, which is very helpful in parsing request parameters. 

4
    url = require("url"); 
5
6
// Create the server. 

7
createServer(function (request, response) { 
8
	// Attach listener on end event. 

9
	request.on('end', function () { 
10
		// Parse the request for arguments and store them in _get variable. 

11
		// This function parses the url from request and returns object representation. 

12
		var _get = url.parse(request.url, true).query; 
13
		// Write headers to the response. 

14
		response.writeHead(200, { 
15
			'Content-Type': 'text/plain' 
16
		}); 
17
		// Send data and end response. 

18
		response.end('Here is your data: ' + _get['data']); 
19
	}); 
20
// Listen on the 8080 port. 

21
}).listen(8080);

This code uses the parse() method of the url module, a core Node.js module, to convert the request's URL to an object. The returned object has a query property, which retrieves the URL's parameters. Save this file as get.js and execute it with the following command:

1
node get.js

Then, navigate to http://localhost:8080/?data=put_some_text_here in your browser. Naturally, changing the value of the data parameter will not break the script.

Reading and Writing Files

To manage files in Node, we use the fs module (a core module). We read and write files using the fs.readFile() and fs.writeFile() methods, respectively. I will explain the arguments after the following code:

1
// Include createServer,

2
import { createServer } from "http"
3
// And fs functions

4
import { readFile, writeFile } from "fs"
5
6
// Create the http server.

7
createServer(function (request, response) {
8
	// Attach listener on end event.

9
	request.on("end", function () {
10
		// Read the file.

11
		readFile("test.txt", 'utf-8', function (error, data) {
12
			// Write headers.

13
			response.writeHead(200, {
14
				'Content-Type': 'text/plain'
15
			});
16
			// Increment the number obtained from file.

17
			data = parseInt(data) + 1;
18
			// Write incremented number to file.

19
			writeFile('test.txt', data);
20
			// End response with some nice message.

21
			response.end('This page was refreshed ' + data + ' times!');
22
		});
23
	});
24
// Listen on the 8080 port.

25
}).listen(8080);

Save this as files.js. Before you run this script, create a file named test.txt in the same directory as files.js.

This code demonstrates the fs.readFile() and fs.writeFile() methods. Every time the server receives a request, the script reads a number from the file, increments the number, and writes the new number to the file. The fs.readFile() method accepts three arguments: the name of the file to read, the expected encoding, and the callback function.

Writing to the file, at least in this case, is much simpler. We don't need to wait for any results, although you would check for errors in a real application. The fs.writeFile() method accepts the file name and data as arguments. It also accepts third and fourth arguments (both are optional) to specify the encoding and callback function, respectively.

Now, let's run this script with the following command:

1
node files.js

Open it in the browser at http://localhost:8080 and refresh it a few times. Now, you may think that there is an error in the code because it seems to increment by two. This isn't an error. Every time you request this URL, two requests are sent to the server. The first request is automatically made by the browser, which requests favicon.ico, and of course, the second request is for the URL (http://localhost:8080).

Even though this behavior is technically not an error, it is behavior that we do not want. We can fix this easily by checking the request URL. Here is the revised code:

1
// Include createServer,
2
import { createServer } from "http"
3
// And fs functions
4
import { readFile, writeFile } from "fs"
5
6
// Create the http server.
7
createServer(function (request, response) {
8
	// Attach listener on end event.
9
	request.on('end', function () {
10
		// Check if user requests /
11
		if (request.url == '/') {
12
			// Read the file.
13
			readFile('test.txt', 'utf-8', function (error, data) {
14
				// Write headers.
15
				response.writeHead(200, {
16
					'Content-Type': 'text/plain'
17
				});
18
				// Increment the number obtained from file.
19
				data = parseInt(data) + 1;
20
				// Write incremented number to file.
21
				writeFile('test.txt', data);
22
				// End response with some nice message.
23
				response.end('This page was refreshed ' + data + ' times!');
24
			});
25
		} else {
26
			// Indicate that requested file was not found.
27
			response.writeHead(404);
28
			// And end request without sending any data.
29
			response.end();
30
		}
31
	});
32
// Listen on the 8080 port.
33
}).listen(8080);

Test it now; it should work as expected.

Accessing MySQL Databases

Most traditional server-side technologies have a built-in means of connecting to and querying a database. With Node.js, you have to install a library. For this tutorial, I've picked the stable and easy-to-use node-mysql. The full name of this module is mysql@2.0.0-alpha2 (everything after the @ is the version number). Open your console, navigate to the directory where you've stored your scripts, and execute the following command:

1
npm install mysql

This downloads and installs the module, and it also creates the node_modules folder in the current directory. Now let's look at how we can use this in our code; see the following example:

1
// Include http module, 
2
import { createServer } from "http"
3
// And mysql module you've just installed. 
4
import * as mysql from "mysql"
5
	 
6
// Create the connection. 
7
// Data is default to new mysql installation and should be changed according to your configuration. 
8
const connection = mysql.createConnection({ 
9
	user: "root", 
10
	password: "", 
11
	database: "db_name"
12
}); 
13
14
// Create the http server. 
15
createServer(function (request, response) { 
16
	// Attach listener on end event. 
17
	request.on('end', function () { 
18
		// Query the database. 
19
		connection.query('SELECT * FROM your_table;', function (error, rows, fields) { 
20
			response.writeHead(200, { 
21
				'Content-Type': 'x-application/json' 
22
			}); 
23
			// Send data as JSON string. 
24
			// Rows variable holds the result of the query. 
25
			response.end(JSON.stringify(rows)); 
26
		}); 
27
	}); 
28
// Listen on the 8080 port. 
29
}).listen(8080);

Querying the database with this library is easy; simply enter the query string and callback function. In a real application, you should check if there were errors (the error parameter will not be undefined if errors occurred) and send response codes dependent upon the success or failure of the query. Also note that we have set the Content-Type to x-application/json, which is the valid MIME type for JSON. The rows parameter contains the result of the query, and we simply convert the data in rows to a JSON structure using the JSON.stringify() method.

Save this file as mysql.js, and execute it (if you have MySQL installed, that is):

1
node mysql.js

Navigate to http://localhost:8080 in your browser, and you should be prompted to download the JSON-formatted file.

Conclusion

Node.js requires extra work, but the payoff of a fast and robust application is worth it. If you don't want to do everything on the lowest level, you can always pick a framework, such as Express, to make it easier to develop applications.

Node.js is a promising technology and an excellent choice for a high-load application. It has been proven by corporations, like Microsoft, eBay, and Yahoo. If you're unsure about hosting your website or application, you can always use a cheap VPS solution or various cloud-based services, such as Microsoft Azure and Amazon EC2. Both of these services provide scalable environments at a reasonable price.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.