Node.js Examples – Basic Examples, Module Examples, Advanced Examples

Node.js Examples

Node.js Examples : We shall go through examples of basics, fs module, mysql module, http module, url module, parsing json, etc. with Node.js.

Following is the list of Node.js Examples we shall go through in this Node.js Tutorial :

Module/TopicExamples
Basics
File System
MySQL
URL
JSON
HTTP

 

Node.js Example : Simple Node.js Example

Following is a simple Node.js Example to print a message to console.

 

Node.js Example : Create a Module

Following is Node.js Example where we create a Calculator Node.js Module with functions add, subtract and multiply. And use the Calculator module in another Node.js file.

 

Node.js Example : Create a File

Following Node.js Example creates a file with data provided.

Run the program using node command in terminal or command prompt :

The file should be created next to your example node.js program with the content ‘Learn Node FS module’.

 

Node.js Example : Read a File

Run the program using node command in terminal or command prompt :
 

Node.js Example : Delete a File

make sure there is a file named ‘sample.txt’ next to the node.js example program.

Run the program using node command in terminal or command prompt :

The file is successfully deleted.

 

Node.js Example : Write to a File

In this example, we shall write content, “Hello !” , to a text file sample.txt.

When the above program is run in Terminal,

 

NodeJS Example – Connect to MySQL Database

 

NodeJS Example – SELECT FROM Table

 

 

NodeJS Example – SELECT from Table with WHERE clause

We shall apply a filter based on marks and fetch only those records with marks greater than 90.

Open a terminal from the location of above .js file and run selectFromWhere.js Node.js MySQL example program.

 

NodeJS Example – ORDER entries BY a column

An example to sort entries in ascending order w.r.t a column.

Run the above Node.js MySQL ORDER BY example program.

The records are sorted in ascending order with respect to marks column.

 

NodeJS Example – INSERT entries INTO Table

Run above Node.js MySQL program in Terminal.
 

Node.js Example – UPDATE Table Entries

Run the above program in Terminal
 

Node.js Example – DELETE Table Entries

Execute DELETE FROM query on specified table with filter applied on one or many properties of records in the table.

 

Node.js Example – Using Result Object

We can access the records in Result Set as an array and properties of a record using DOT (.) Operator.

Run the above program using node in Terminal

 

Node.js Example – Parse URL Parameters

 

Node.js Example : Parse JSON File

Following example helps you to use JSON.parse() function and access the elements from JSON Object.

 

Node.js Example : Create HTTP Web Server

Node.js Example  – A HTTP Web Server that prepares a response with HTTP header and a message.

Run the Server

Open a browser and hit the url, “http://127.0.0.1:9000/”, to trigger a request to our Web Server.

Create HTTP Web Server in Node.js