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

The Characteristics of a Successful Agile Coach

1. Patience An agile coach, just like any coach, needs to be patient with the people they are leading. If you want your business to be agile, then you need  everyone in your company  to fully-understand what you’re working towards, and not everyone is going to “get it” straight away. Ultimately, you’re probably going to be asked the same question a thousand different times, and you need to figure out how to keep your cool. 2. Flexibility The agile framework can’t exist without flexibility. The whole point of being “agile” in the business world, is being able to adapt to changes in your marketplace. In other words, as a coach, you’ll need to be prepared for whatever happens in your organization, and have a certain strength about you when it comes to rolling with the punches. There will be times when you face challenges that leave you scratching your head and wondering what to do next. However, whenever you encounter a new scenario, you need to be able to take your trie...

Agile Metrics

Know your business "Done" only tells half the story. It's about building the right product, at the right time, for the right market. Staying on track throughout the program means collecting and analysing some data along the way. In any agile program, it's important to track both business metrics and agile metrics. Business metrics focus on whether the solution is meeting the market need, and agile metrics measure aspects of the development process. A program's business metrics should be rooted in its roadmap. For each initiative on the roadmap, include several key performance indicators (KPIs) that map to the program's goals. In addition, include success criteria for each product  requirement  such as adoption rate by end-users or percentage of code covered by automated tests. These success criteria feed into the program's agile metrics. And the more teams learn, the better they can adapt and evolve.  How to use agile metrics to optimi...

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...