Skip to main content

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.

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

Get Requests

Get requests are those which request a site for a specified resource or some data.
In this Node.js Tutorial, we shall learn how to handle Get Requests to other websites from our HTTP Web Server in Node.js using request module.
To learn about creating HTTP Web Server, please refer Create a HTTP Web Server in Node.js.

Handle Get Requests using request Node.js module

There is module called ‘request’ for Node.js, which could help us to make get requests to another website. We shall start with installing the request Node.js module.

Install ‘request’ Node.js Module

Open a Terminal or Command prompt and run the following command to install request Node.js module
$ npm install request

Example Node.js file with Get Request

Following is an example Node.js file in which we shall include request module. And make a get request for the resource “http://www.google.com”. Call back function provided as second argument receives error(if any), response and body.
Run the above Node.js file in the terminal as shown in the following
$ node serverGetRequests.js
The response would be echoed back to the console.
If there is no error with the get request, the content of error would be null. This information could be used as a check if there is any error in the get request made for a resource.

Example Node.js file with Get Request receiving error

There could be scenarios during which we might get an error for Get Request we do for a resource. Following example is a scenario of such where the url provided is bad.



Comments

Popular posts from this blog

ACP Exam tips

       1.    Time: The 3 hours to answer 120 questions were sufficient. I finished answering all questions in 2 hours. There were 4-5 questions not framed well (poor English). I spend 1 hr to review all my answers again. 2. Agile Practices: There were multiple questions on SCRUM and XP, very few questions on Kanban and Lean, almost none on Crystal, FDD, and DSDM. 3. Roles: Completely Understand the Roles in SCRUM and XP, what are the responsibilities carried by those roles, various meetings, input and outcome of those meetings like review, retrospective etc, 4. Scenario Based Questions: There were multiple questions based on scenarios and how best will you apply agile practices under those scenarios. Mostly on Team,Scrum,Communication stuff. 5. Completely understand the when and what use of Scrum Artifacts and ceremony. 6. . Many questions on team Collaboration, Risk,Velocity. 7. Few Question on EVM,EV,PV (no calculation). 8. Multiple quest...

The advantage of ReactJs:

SEO-friendly:  ReactJs is very comfortable with the SEO. You can easily run your ReactJs with the servers while other Javascript doesn’t support SEO. JSX : In ReactJs for templating we use JSX. JSX is simple JavaScript which allows HTML syntax and other HTML tags in the code. HTML syntax is processed into javascript calls of React framework. React Native:  It contains a native library which supports Native iOS, Android application. Simplicity : It is a very simple to grab. Its component-based approach and well-defined lifecycle are very simple to use. Easy to learn:  Anyone with the basic knowledge of programming can easily ReactJs. For Learning ReactJs you just need to know the basics for HTML and CSS. Data-Binding:  ReactJs uses one-way data binding and application architecture controls the flow of data via a dispatcher. Testability : ReactJs application is very easy to test. Its views are very easy to configure and can be treated as the a...

PMI Agile Certified Practitioner (PMI-ACP®) Certificate

This post is for  Project Management Institute (PMI)’s Agile Certified Practitioner(PMI-ACP) Certificate . Exam for this certificate is meant to test skills of agile practitioners.  Pre-requisite  for appearing in the exam includes having prior experience in general project management (2000+ hours) and agile specific project management (1500+ hours). Folks having prior  PMP ®  or PgMP ®  automatically satisfy general project management experience and hence need not elaborate the experience while filling the form. Also pre-requisite requires having 21 contract hours earned in agile practice, which could include imparting or participating in agile training [Certified Scrum Master (CSM®), In-house agile training, Coaching Client, etc.] My experience of the exam includes following tips 1. This exam tests knowledge on tools, techniques, processes, artifacts, etc. of those practicing agile. It includes knowledge on Scrum, XP, Kanban & L...