This tutorial will help you understand the basics of Node.js. After reading this tutorial, you should be able to take your knowledge further and build some cool applications. At the end of this tutorial, there will be a small snippet on sending emails from your Node.js server. We shall also look at how Node.js can be used with Raspberry Pi.
Contents
- What is Node.js?
- Installing Node.js
- Getting Started
- HTTP Module
- NPM
- File System
- Events
- URL Module
- Sending E-Mails with Node.js
- Node.js on Raspberry Pi
- Conclusion
What is Node.js?
Node.js is a powerful open-source, cross-platform, JavaScript runtime environment that executes code outside of web browsers. It is built on Google Chrome’s V8 JavaScript engine. It was originally written by Ryan Dahl in 2009. Node.js allows server-side scripting using JavaScript. Node.js is single-threaded, meaning that one command is executed at a time. JavaScript is one of the most popular programming languages on the planet. This makes Node.js even more attractive to developers and it has an extensive worldwide developer community.
Node.js natively supports only JavaScript. It follows an asynchronous event-driven architecture that allows you to build scalable networking applications. It is single-threaded, as stated before. While it cannot make use of multiple processor cores, it reduces programming complexity as it takes expert programming to program multi-threaded processes. It comes with a lot of core functionality with many modules for networking like HTTP, DNS, TCP; file system I/O, data streaming among others. This makes the building of web servers much easier.
We shall be looking at the basic modules of Node.js and how to write simple server-side applications. First, we shall install the Node.js environment in our system.
Installing Node.js
Installing Node.js is a simple process. Go to the official downloads page and download the relevant installer based on the operating system you are using.
Windows and macOS
Download the executable file and run it.
In the above image, make sure that the Add to PATH option is enabled. Continue the installation. Once you are done, open a terminal (or command prompt in Windows) and type the following:
If you see the version matching the version that you installed, then your installation was correct and you are good to go!
Linux
Visit the nvm GitHub repository page and copy either the curl
or wget
command to download and install the nvm
script:
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
The script will clone the project’s repository from Github to the ~/.nvm directory. Once the script is in your PATH
, verify that nvm
was properly installed by typing:
Once nvm is installed, type the following to get the list of all the available node.js versions:
nvm ls-remote
From the list of available versions, choose the version you want and type the following:
nvm install version_number
After the installation is complete, simply open a terminal and type node
This will open a comman- line environment where you can type in some code. We shall not be using this environment in our tutorial.
To quit the node terminal simply press Ctrl C twice.
Another important package that usually comes with the Node.js installation is the Node Package Manager (NPM). With NPM you can install new modules that you will use in your applications. A simple way to check if NPM was installed in your system is to go to a terminal and type the following:
If you see the version (you should), then you can proceed further in the tutorial.
Now that we have completed the installation of the environment, we can proceed with different modules.
Getting Started
We shall be running our web server applications from the Command-Line. It is recommended that you make a folder in your computer that contains all your JavaScript files. To run any file, navigate to that file using the command line and then type:
node filename.js
Node.js HTTP Module
This is one of the basic built-in modules present in Node.js. You can use this module to make your first web server. It is a tradition to print that “Hello, World!” statement when you learn a new programming language. Look at the code below.
//hello.js
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/html'});
response.end("Hello, World!");
}).listen(5000);
//prints the text to the console
console.log('Server running at http://127.0.0.1:5000/');
- The first statement shows the declaration of a variable ‘http’ using the var keyword. Similar to the “import” keyword in other languages, here we import the HTTP module in our code using the require() function.
- Then we create a server object using http.createServer(). The function that we pass to this server object is executed when the server gets a request on port 5000.
- 200 is the HTTP status code for “OK” and the second argument is the type of the response object.
- We end the response using the response.end() function after it prints the “Hello, World!” message to the screen.
Congratulations! You have successfully created your first web server using Node.js.
Go to a terminal, navigate to the directory where your hello.js file is saved and type:
node hello.js
Open a browser and type ‘127.0.0.1:5000’ and you should see the following output:
Node.js NPM
We already saw what NPM is and what we can use it for. Let us now look at an example of how to install a new module. Note that all the modules are locally installed in a new folder called node_modules which is located within your main folder that contains all your JavaScript files.
We shall see a simple example in which we install the cities module and find a particular city by just typing the zip code. Note that this module currently supports only US cities. Type the following line in a terminal:
npm install cities
Now if you go to the node_modules folder you should see the cities module present.
Type the following code to print the State and City corresponding to the zip code entered as the zip_lookup function argument.
//StateCity.js
var http = require('http');
var cities=require('cities');// import the cities module
city =cities.zip_lookup(80205);//any valid US zip code
console.log(city)
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/html'});
response.write("State: "+city.state+'<br>'+"City: "+city.city);//<br> tag is a HTML line-break tag
// city is dictionary with state and city as keys
response.end();
}).listen(5000);
Run the application and you should see the output in a browser window:
We can also update existing modules using the following command:
npm update module_name
You can also use the uninstall function to uninstall modules:
npm uninstall module_name
Node.js File System
Before we delve deeper into the file system module, we need to understand the difference between synchronous and asynchronous programming in Node.js:
Synchronous:
Here, the program will perform blocking operation. This means that the program blocks the first line and waits for it to be executed before it proceeds to the next line.
Asynchronous:
Here, the program does not block the code. If the current line is being executed, it will proceed to the next line and execute that while the current line is being executed in the background. Once the current line finishes its execution, the result is displayed.
The difference might sound a little vague. For better clarity, look at the example below:
First, create a file called example.txt in your node.js folder and paste the following content:
IoTEDU is here to teach you the basics of Node.js!
\yourfolder\file.js
var fs = require("fs"); //we are importing the file system module
// Asynchronous read
fs.readFile('example.txt', function (err, data) {
if (err) {
return console.error(err);
}
console.log("Asynchronous read: " + data.toString());
});
// Synchronous read
var data = fs.readFileSync('example.txt');
console.log("Synchronous read: " + data.toString());
console.log("Program Ended");
Look at the output of the above code:
As we can see, while the asynchronous file read is taking place, the program performs the synchronous file read and also the next statement in sequence. Once the asynchronous file read is done, the output of that is displayed at the bottom. You can note that the whole asynchronous code is written within a set of curly braces to differentiate from the synchronous code.
It is always recommended to use the asynchronous mode as it never blocks the calling thread and executes whichever instruction gives the result first.
Now let us look at a few common file system uses.
Creating Files
You can create files using any of the three functions listed below:
- fs.open()
- fs.appendFile()
- fs.writeFile()
In all three cases, if the file does not exist, the program will create a file in the same folder as your Node.js files. Check out the examples below:
// \yourfolder\open.js
var fs = require('fs'); //importing the file system module
fs.open('firstfile.txt', 'w', function (err, file) {
if (err) throw err;
});
If firstfile.txt does not exist, the program creates the file. The second argument ‘w’ is a flag that specifies that we shall write to the file that we have opened. So now let us write some content to the file.
// \yourfolder\write.js
var fs = require('fs');
fs.writeFile('firstfile.txt','Welcome to IoTEDUs Node.js tutorial!',function(err,file) {
if (err) throw err;
})
Similarly, using the fs.appendFile() function appends the new content to the end of the file. Note that if you use the writeFile() function to write some content to an existing file, the previous content gets overwritten.
Reading Files
// \yourfolder\read.js
var fs = require('fs')
fs.readFile('example.txt', (err, data) => {
if (err) throw err;
console.log(data.toString());
});
If you created and ran the open.js and write.js files, now you can run the read.js file. This is the output:
Deleting Files
You can delete files using the following code:
var fs = require('fs');
// we use the unlink function to delete existing files
fs.unlink('firstfile.txt', function (err) {
if (err) throw err;
console.log('File deleted!');
});
Renaming FIles
var fs = require('fs');
// we use the rename function to rename the file
fs.rename('firstfile.txt','secondfile.txt', function (err) {
if (err) throw err;
console.log('File renamed!');
});
Node.js Events
Now let us look at an important module that comes with the Node.js installation – the Events module.
We saw earlier in the tutorial that Node.js is highly event-driven. Everything from reading to creating files can be considered as an event. Node.js comes with a built-in ‘events’ module. Look at the following example:
// Import events module
var events = require('events');
// Create an eventEmitter object
var eventEmitter = new events.EventEmitter();
We can bind the event to an even handler as follows:
eventEmitter.on('eventName', eventHandler);
We can programmatically fire an even as follows:
eventEmitter.emit('eventName');
Let us look at a short example:
var events = require('events');
var eventEmitter = new events.EventEmitter();
//Create an event handler:
var myEventHandler = function () {
console.log('This is my first event');
}
//Assign the event handler to an event:
eventEmitter.on('first', myEventHandler);
//Fire the 'first' event:
eventEmitter.emit('first');
When you save and run the code, you should see the following output:
This is my first event
Methods of the EventEmitter() Class:
Let us now look at some of the properties that we get with the EventEmitter() class:
Sr. No. | Properties with description |
1. | addListener(eventName, listener) Adds a listener to the array of listeners for the particular event. |
2. | emit(eventName[, …args]) Synchronously calls each of the listeners registered for the event named eventName, in the order they were registered, passing the supplied arguments to each. Returns true if the event had listeners, false otherwise. |
3. | listeners(eventName) Returns a copy of the array of listeners for the event named eventName. |
4. | on(eventName, listener) Adds the listener function to the end of the listeners array for the event named eventName. |
5. | off(eventName, listener) Removes the specified listener from the listener array for the event named eventName |
6. | once(eventName, listener) Adds a one-time listener function for the event named eventName. The next time eventName is triggered, this listener is removed and then invoked. |
7. | removeListener(eventName, listener) Removes the specified listener from the listener array for the event named eventName. |
8. | removeAllListeners([eventName]) Removes all listeners, or those of the specified eventName. |
9. | setMaxListeners(n) By default, EventEmitters will print a warning if more than 10 listeners are added for a particular event. This is a useful default that helps to find memory leaks. The value can be set to Infinity to indicate an unlimited number of listeners. |
10. | prependListener(eventName, listener) Adds the listener function to the beginning of the listeners array for the event named eventName. |
Create a file called listen.js:
var events = require('events');
var eventEmitter = new events.EventEmitter();
// listener #1
var listener1 = function listener1() {
console.log('listener1 executed.');
}
// listener #2
var listener2 = function listener2() {
console.log('listener2 executed.');
}
// Bind the connection event with the listner1 function
eventEmitter.addListener('connection', listener1);
// Bind the connection event with the listener2 function
eventEmitter.on('connection', listener2);
// Fire the connection event
eventEmitter.emit('connection');
// Remove the binding of listner1 function
eventEmitter.removeListener('connection', listener1);
console.log("Listener1 will not listen now.");
// Fire the connection event
eventEmitter.emit('connection');
console.log("End")
This is how the output will look like:
Node.js URL Module
The URL module provides utilities for URL resolution and parsing. We can split a web address into readable parts. Look at the following example:
var url = require('url'); //this is how we import the URL module
var adr = 'https://iot4beginners.com/about/';
var q = url.parse(adr, true);
console.log(q.host);
console.log(q.pathname);
The console will print:
iot4beginners.com
/about/
To better explain a few features available with this module, look at the example code below:
var url = require('url');
var adr = 'https://abc:xyz@example.com:443/abc?123';
//Parse the address:
var q = url.parse(adr, true);
/*The parse method returns an object containing url properties*/
console.log(q.host); // return hostname with port
console.log(q.pathname); //returns path name
console.log(q.port); // returns port number
console.log(q.protocol);// returns protocol
console.log(q.search);//returns search string after ?
Sending E-Mails with Node.js
Now, as I promised before, we shall learn how to send E-Mails using Node.js. I shall show you how to send mails using Gmail. First, we need to install the “nodemailer” module:
npm install nodemailer
Now copy the code below onto a file mail.js:
var nodemailer = require("nodemailer"); //import the nodemailer class
// async..await is not allowed in the global scope, must use a wrapper
async function main() {
// create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport({
service:'gmail',
port: 465,
secure: true, // true for 465, false for other ports
auth: {
user: 'your_id@gmail.com', //enter your gmail id
pass: 'your_password', // enter your password
},
});
// send mail with defined transport object
var info = await transporter.sendMail({
from: '"Your Name" <your_id@gmail.com>', // sender address
to: "Receiver_1, Receiver_2", // list of receivers. For multiple receivers, simply separate the addresses using commas
subject: "Hello ✔", // Subject line
text: "Node.js Test Mail", // plain text body
html: "<b>Welcome to our mail testing service!</b>", // html body
});
console.log("Message sent: %s", info.messageId);
// Message sent
}
main().catch(console.error);
I tested it using my mail id and this is the output:
And voila! Using this simple code, we can build some automatic mail sending applications for important notices or inviting a specific list of people to a party.
Node.js on Raspberry Pi
Coming to the last part of this tutorial, Node.js on Raspberry Pi. Check out my earlier post on installing Node.js on Raspberry Pi. Once you have successfully installed Node.js on Raspberry Pi, you can check the example below.
First, you need to install the ‘onoff’ module through which we control the GPIO pins of the Raspberry Pi:
npm install onoff
Parts Required
- Raspberry Pi
- 1x LED
- 2x Female to Male jumper wires
- 1x 220 ohm resistor
Make the connections as shown below:
Similar to what we have done before, let us create a directory to store all our Node.js files:
cd Documents
mkdir nodefiles
Go to the above directory from a terminal and open a nano file:
sudo nano blinkled.js
var Gpio = require('onoff').Gpio; //include onoff to interact with the GPIO
var LED = new Gpio(4, 'out'); //use GPIO pin 4, and specify that it is output
var blinkInterval = setInterval(blinkLED, 250); //run the blinkLED function every 250ms
function blinkLED() { //function to start blinking
if (LED.readSync() === 0) { //check the pin state, if the state is 0 (or off)
LED.writeSync(1); //set pin state to 1 (turn LED on)
} else {
LED.writeSync(0); //set pin state to 0 (turn LED off)
}
}
function endBlink() { //function to stop blinking
clearInterval(blinkInterval); // Stop blink intervals
LED.writeSync(0); // Turn LED off
LED.unexport(); // Unexport GPIO to free resources
}
setTimeout(endBlink, 5000); //stop blinking after 5 seconds
Save the file using Ctrl + X and then Y.
node blinkled.js
You should see the LED blinking now. Similarly, you can use pushbuttons and other components as well. You can create a web server to monitor and control lights in your house. There are many amazing things you can do with Node.js. The sky is the limit.
Conclusion
With that, we have reached the end of this tutorial on Node.js. I hope you learned something useful and I will be happy if you create something cool with Node.js with the knowledge you gained here.
Keep learning!