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

REACTJS FOR BEGINNERS | A STEP BY STEP APPROACH

There are many problems while building large applications with data that changes over time. To solve this ,I suggest checking out  ReactJS . React lets you express how your app should look at any given point, and can automatically manage all UI updates when your underlying data changes. React  is one of the most popular JavaScript front end libraries which is developed by Facebook. It’s used for handling view layer for web and mobile apps. The main feature of ReactJS is that it allows us to create reusable UI components.  The syntax used in React is JSX which allows you to mix HTML with JavaScript. This is not a requirement – you can still write in plain JavaScript. But this is suggested because this makes writing your components a breeze. Installation To install React with Yarn, run: 1 2 yarn init yarn add react react - dom To install React with npm, run: 1 2 npm init npm install -- save react react - dom The bundlers like  webpa

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.

20 Blockchain Use Cases for 2018 You Should Know

Crypto markets get bloodier by the day. But does it mean, that the blockchain technology failed? 1. Real estate Brickblock  tokenized the first real estate on the blockchain.  Indiegogo  is  tokenizing  real estate shares. Photo by  Element5 Digital  on  Unsplash 2. Voting 150  West Virginians   voted  via a blockchain-app.  South Korea   is testing  E-Voting on the blockchain. Japanese city  Tsukuba   completed  the test of a blockchain-based voting system for local development programs. Photo by  Element5 Digital  on  Unsplash 3. Identity solutions Swiss Federal Railways are  testing  the  uPort  identity solution. City of Antwerp is  testing   Jolocom  for the administrative tasks.  MONI  is  providing  a blockchain powered id for refugees. Photo by  Roland Lösslein  on  Unsplash 4. Equity token offerings NEUFUND  listed the first companies on it’s ETO (equity token offerings) platform. Recently they  raised  $3m in an ETO. Photo by  M. B