This article provides a basic tutorial of NodeJs.Here, we will start with the simple NodeJs introduction and sample program and then leverage the discusion with the different modules such as HTTP, event, mysql, mongoDB available in NodeJs with the some simple use case and example code to make use of those modules in real time NodeJs application.
Following is the simple way to create a Node Js server.Node Js has a built in module called HTTP and using this module we can create Node server.Following sample creates a Node server that listens to HTTP request on port 8080. So, whenever a HTTP request comes to port 8080 Hello World response is sent back to the client.
To execute above script you can save the file(sampleserver.js) containing above script on your local machine and execute it using following command.
Node Js has primarily 3 different modules - Core Module, Local Module and Third party Module. Core module is the module that is included in the Node Js binary distribution by default and load automatically when Node process starts. For example - http,url, queryString, path, fs etc. Similarly, local modules are custom modules created locally in our code. We generally include common application functionality in a local module for reuse.
Core module is the module that is included in the Node Js binary distribution by default and load automatically when Node process starts.Different ore module provied by angular are http,url, queryString, path, fs
var module = require('http'); loads HTTP core module. Following is an example to work with url module provided by Node.
Above line prints the host, pathname and search parameter in the console.
Local modules are custom modules created locally in our code to moduarise the common functionality such as following is a local module example that Date object.datetimemodule.js
This module can be included as - var dt = require('./datetimemodule.js');
Any action is an event and node provides event module to handle events.Node provides ways to either create your own event using events module or can use exiting events on any node object such as following is an example to raise event on opening and closing a file.
It is also possible to create a custom event in Node.Following is an example to raise and handle custom event.
In the above example whenever myEvent is raised, the log will be printed in the console.
Firt, we require to install mysql driver to access a MySQL database from node application. To install MySQL driver, run following command
Once this is done, you can include this mysql module in you existing js file to connect to a database and execute sample query.
npm install mongodb installs driver for mongoDB in node and once this is done we can use this module to communicate with the MongoDB database.Following is an example to connect to testdb and fetch user.
I hope this tutorial helped in building basic idea of Node Js. In the next tutorial, we will look into configuring express.js with node for API development.If you have anything that you want to add or share then please share it below in the comment section.
1. Rxjs Tutorial
2. Angular Material App
3. Typescript Tutorial
4. Install Nginx Php Mac
Contact us: firstname.lastname@example.org