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

PMI-ACP notes

Agile Tooling Agile Tooling refers to hi-tech or low-tech software or artifacts designed to increase the sense of team and to encourage participation among the members. Examples include version control software, collaboration software, or video conferencing for distributed teams. wireframe A wireframe is a "low fidelity” prototype. This non-graphical artifact shows the skeleton of a screen, representing its structure and basic layout. It contains and localizes contents, features, navigation tools and interactions available to the user. It is often used as a communication tool serving as an element of conversation and confirmation of Agile user stories. Domain Model Domain Model illustrates: the business structure, its entities, and their relationships . supply system A supply system where items are produced at the request of the customer is called a Pull System. Ethnocentrism An attitude that one's group is superior to others is called an Ethnocentrism....

Agile metric to measure agile success

#1 On-Time Delivery According to the  State of Agile survey , 58% of the respondents* said they measured the success of their agile initiatives by on-time delivery. With agile, our schedule is fixed and our scope is flexed. What does that mean for on-time? Well, time just happens, so theoretically, we are always on time. But, on-time is generally measured in context with the expectations about what will be delivered. To measure and have visibility of what is being delivered, we may look to the out-of-the box metrics of  the burndown or the burnup . For instance, in this VersionOne burndown chart you can see progress as the team heads toward an expected end date. This burnup chart, on the other hand, allows you to see the trend of getting stuff done, as well as the impact of scope changes. #2 Product Quality A total of 48% of the respondents to the survey said they measured the success of their agile initiatives through product quality. Quality is often...