This article is an easy guide to write and run your first Node.js program on a Raspberry Pi system.
Write and Run Node.js Program on Raspberry Pi for the First Time?
To write and run the Node.js program on Raspberry Pi, there are some prerequisites which need to be fulfilled and those include installing Node.js and Node Package Manager (NPM) on Raspberry Pi.
So for a complete process of writing and running a Node.js program, follow the steps mentioned below:
Step 1: Update/Upgrade Raspberry Pi Repository
First update the Raspberry Pi repository to get the latest version of packages available in the repository. To check for packages update in the repository, run the below-mentioned command:
Then upgrade the repository using the below-written command:
Step 2: Install Node.js on Raspberry Pi
To install the Node.js package on Raspberry Pi, use the following command:
Step 3: Verifying Installation
After installing the Node.js package, run the below-written command to verify the installation:
The command will display the installed version of node.js as an output:
The NPM is also installed with Node.js and you can confirm it using the following command:
Step 4: Write First Node.js Program
To write the very first Node.js program, open the nano editor and name your file according to your choice:
Syntax
Example
Now inside the file, write your first program to print the message and for that use the following code:
Syntax
Example
“Console” is the object in Node.js and to print the desired message/string with Node.js, the log is used:
Then save the file using Ctrl+X.
Step 5: Run the Node.js Program
To run the Node.js program, simply write the below-mentioned command along with the name of your Node.js file that has been created in the previous step:
Syntax
Example
The output will be displayed on the terminal:
In this way, you can write and run different Node.js programs on Raspberry Pi.
Create First Node.js Application and Run it on the Server
After writing the first Node.js program, let’s create a Node.js server-based application and for that, follow the below-mentioned steps:
Step 1: For creating a Node.js web server, let’s create another .js file using nano editor:
Syntax
Example
Step 2: Now within the file, import the “http” module and will store the returned HTTP instance in a variable http:
Then we will create a server to send a response and print our message on the console. Add the following lines inside the linuxhint.js file:
// Send the HTTP header
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});
// To send the response body
response.end('\n');
}).listen(8081);
// to print the message on console
console.log('Server running at http://:8081/');
The user can change the message according to desirability:
Save the file by pressing keys Ctrl+X then Y.
Step 3: Now run the .js file using the following command:
Step 4: Now open the browser and write the server address that appears in the above command:
Note: Add your own Raspberry Pi IP address instead of “192.168.18.2”, which you can find by running the “hostname -I” command. You can add the address on any system browser.
Conclusion
To write the first Node.js program, you have to install Node.js on Raspberry from the official repository of Raspberry Pi. After the installation, by using the nano editor can create the .js file and add the Node.js program into the file and run the file using the node command. You can also create a Node.js server-based application through the same procedure. However, you have to do some configuration within the .js file to access the application on the web using your Raspberry Pi IP address.