0% found this document useful (0 votes)
16 views

UNIT III r20 iot

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

UNIT III r20 iot

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

UNIT III : Node.js: Introduction, Advantages, Node.js Process Model, Node JS Modules. Express.

js:
Introduction to Express Framework, Introduction to Nodejs , What is Nodejs, Getting Started with
Express, Your first Express App, Express Routing, Implementing MVC in Express, Middleware, Using
Template Engines, Error Handling , API Handling , Debugging, Developing Template Engines, Using
Process Managers, Security & Deployment.

Node.js: Introduction

What is Node.js?
 Node.js is an open source server environment
 Node.js is free
 Node.js runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
 Node.js uses JavaScript on the server

Why Node.js?
Node.js uses asynchronous programming!

A common task for a web server can be to open a file on the server and return the content to the client.

Here is how PHP or ASP handles a file request:

1. Sends the task to the computer's file system.


2. Waits while the file system opens and reads the file.
3. Returns the content to the client.
4. Ready to handle the next request.

Here is how Node.js handles a file request:

1. Sends the task to the computer's file system.


2. Ready to handle the next request.
3. When the file system has opened and read the file, the server returns the content to the client.

Node.js eliminates the waiting, and simply continues with the next request.

Node.js runs single-threaded, non-blocking, asynchronous programming, which is very memory efficient.

What Can Node.js Do?


 Node.js can generate dynamic page content
 Node.js can create, open, read, write, delete, and close files on the server
 Node.js can collect form data
 Node.js can add, delete, modify data in your database
What is a Node.js File?
 Node.js files contain tasks that will be executed on certain events
 A typical event is someone trying to access a port on the server
 Node.js files must be initiated on the server before having any effect
 Node.js files have extension ".js

 Download Node.js
 The official Node.js website has installation instructions for Node.js: https://nodejs.org

 Getting Started
 Once you have downloaded and installed Node.js on your computer, let's try to display "Hello World" in a web
browser.
 Create a Node.js file named "myfirst.js", and add the following code:
 myfirst.js
 var http = require('http');

http.createServer(function (req, res) {


res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);
 Save the file on your computer: C:\Users\Your Name\myfirst.js
 The code tells the computer to write "Hello World!" if anyone (e.g. a web browser) tries to access your computer
on port 8080.
 For now, you do not have to understand the code. It will be explained later.

 Command Line Interface
 Node.js files must be initiated in the "Command Line Interface" program of your computer.
 How to open the command line interface on your computer depends on the operating system. For Windows users,
press the start button and look for "Command Prompt", or simply write "cmd" in the search field.
 Navigate to the folder that contains the file "myfirst.js", the command line interface window should look
something like this:
 C:\Users\Your Name>_

 Initiate the Node.js File
 The file you have just created must be initiated by Node.js before any action can take place.
 Start your command line interface, write node myfirst.js and hit enter:
 Initiate "myfirst.js":
 C:\Users\Your Name>node myfirst.js
 Now, your computer works as a server!
 If anyone tries to access your computer on port 8080, they will get a "Hello World!" message in return!
 Start your internet browser, and type in the address: http://localhost:8080

Node.js Advantages
Scalable Web App Development
Node.js was built with scalability in mind. It allows multiple nodes to run simultaneously and interact with each other
which is superior to other web backend development solutions. It has a cluster module that is responsible for load
balancing for every CPU-intensive task. This is one of the main benefits of Node.js. You can run more than one node
at a time and the cross-platform runtime environment will automatically balance the workload.
Node.js allows horizontal partitioning and divides your app into small instances. You can show various versions of the
mobile apps to users based on their interests, age, location, language, etc. which reduces workload and increases
personalization. Node.js child processes operate quickly and interact with each other to share the same origin.

High Performance
Node.js is popular for providing excellent performance and facing considerable amounts of data to process at the
same time. It is a perfect choice for backend development that interprets the JavaScript code using Google’s V8
JavaScript engine. Node.js web app development engine helps to compile the code into machine code directly,
making it easier and faster to write server-side code and implement it.

The backend Node.js technology accelerates code execution and its runtime environment supports non-blocking I/O
operations. As a single-threaded event loop, it uses multiple worker threads in the background to execute the
asynchronous programming code and nonblocking which means the callback function is delegated to the event loop
but executed by different threads

Powerful Data Processing


Node.js is a backend development language and runtime environment capable of processing thousands of requests
simultaneously. It is a perfect choice for efficient data-handling tools. Many backend development platforms use
synchronous processing for handling requests where they each wait to process one by one. It refuses the traditional
system and adopts an asynchronous structure for upcoming requests to process together with the first one.

There are two approaches for software development: linear and event-based programming. A linear tool helps to
execute algorithms in a queue regardless of users’ actions. Event-based programming doesn’t have a specific output
order and there are useful background operations.

Control Flow Features


Node.js is a popular backend development platform because developers see asynchronous nature as both advantage and
disadvantage of Node.js web app development. Whenever a function executes, the code sends a callback
automatically. As the number of functions is increasing, so does the number of callbacks which is known as callback
hell.
The control flow features of Node.js offer a way out. Using a web app framework can help you to map functions and
sort through callbacks. It integrates similar functions automatically and finds necessary features by search or in a
folder.

V8 Engine
As we all know, V8 engines are the best engines out there which were initially developed for Chrome. But the Node.js
development team readapted it to the javascript run time environment purpose. It is written in C++ to compile
functions written in JavaScript into machine code for impressive speed and performance. Thanks to Google for
investing much time and effort in its engine, V8 demonstrates performance enhancements every year and extracts
the whole bag of benefits.

Concurrent Request Handling


With Node.js, handling multiple concurrent users simultaneously at a single go is possible. And this is one of the
biggest advantages of Node.js. Its capacity to handle multiple requests at once for any type of complex and extensive
web application has made it popular among Javascript developers. Besides, the majority of the eCommerce business
prefer Node.js for their application because of this reason.
Lesser Loading Time
Node.js comes with a caching module that helps in reducing the overall loading time. Developers are able to execute
the code without any hassle. Besides, having a caching module helps the web app get a request easily and then
transfer it to in-app memory. This enables the user to get results on web pages in a few microseconds without waiting
much

Node.js Process Model


Node.js Process
Node.js provides the facility to get process information such as process id, architecture, platform, version, release, uptime, upu usage
etc. It can also be used to kill process, set uid, set groups, unmask etc.

The process is a global object, an instance of EventEmitter, can be accessed from anywhere.

Node.js Process Properties

A list of commonly used Node.js process properties are given below.

Property Description

arch returns process architecture: 'arm', 'ia32', or 'x64'

args returns commands line arguments as an array

env returns user environment

pid returns process id of the process

platform returns platform of the process: 'darwin', 'freebsd', 'linux', 'sunos' or 'win32'

release returns the metadata for the current node release

version returns the node version

versions returns the node version and its dependencies

Node.js Process Properties Example

Let's see the simple process example to print architecture, pid, platform and version of the process.

File: process_example1.js

console.log(`Process Architecture: ${process.arch}`);


console.log(`Process PID: ${process.pid}`);
console.log(`Process Platform: ${process.platform}`);
console.log(`Process Version: ${process.version}`);

Open Node.js command prompt and run the following code:

node process_example1.js
Let's see another process example to print command line arguments. Here node is considered as the first argument, filename is
considered as the second argument and actual command line arguments are considered as third, fourth, fifth and so on.

File: process_example2.js

process.argv.forEach((value, index, array) => {


console.log(`${index}: ${value}`);
});

Open Node.js command prompt and run the following code:

node process_example2.js

Node.js Process Functions

A list of commonly used Node.js process functions are given below.

Function Description

cwd() returns path of current working directory

hrtime() returns the current high-resolution real time in a [seconds, nanoseconds]


array

memoryUsage() returns an object having information of memory usage.

process.kill(pid[, is used to kill the given pid.


signal])

uptime() returns the Node.js process uptime in seconds.

Node.js Process Functions Example

Let's see the process example to print current working directory and uptime of the process.
File: process_example3.js

console.log(`Current directory: ${process.cwd()}`);


console.log(`Uptime: ${process.uptime()}`);

Open Node.js command prompt and run the following code:

node process_example3.js

Node.js Child Process


The Node.js child process module provides the ability to spawn child processes in a similar manner to popen(3).

There are three major way to create child process:

o child_process.exec() method: This method runs a command in a console and buffers the output.
o child_process.spawn() method: This method launches a new process with a given command.
o child_process.fork() method: This method is a special case of spawn() method to create child processes.

Node.js child_process.exec() method

The child_process.exec() method runs a command in a console and buffers the output.

Syntax:

child_process.exec(command[, options], callback)

Parameters:

1) command: It specifies the command to run, with space-separated arguments.

2) options: It may contain one or more of the following options:

o cwd: It specifies the current working directory of the child process.


o env: It specifies environment key-value pairs.
o encoding: String (Default: 'utf8')
o shell: It specifies string Shell to execute the command with (Default: '/bin/sh' on UNIX, 'cmd.exe' on Windows, The shell
should understand the -c switch on UNIX or /s /c on Windows. On Windows, command line parsing should be compatible
with cmd.exe.)
o timeout: Number (Default: 0)
o maxBuffer: Number (Default: 200*1024)
o killSignal: String (Default: 'SIGTERM')
o uid Number: Sets the user identity of the process.
o gid Number: Sets the group identity of the process.

callback: The callback function specifies three arguments error, stdout and stderr which is called with the following output when
process terminates.

Node.js child_process.exec() example 1

Let's see the simple process example to print architecture, pid, platform and version of the process.

File: child_process_example1.js

const exec = require('child_process').exec;


exec('my.bat', (err, stdout, stderr) => {
if (err) {
console.error(err);
return;
}
console.log(stdout);
});

Create a batch file named my.bat having the following code:

File: my.bat

1. dir
2. mkdir child

Open Node.js command prompt and run the following code:


node child_process_example1.js
It will execute two commands dir and mkdir child. The dir command will display list of current directory and mkdir command will
create a new directory. For linux, you can you ls command to display the current directory list.
It will create a new directory also.

Node.js child_process.exec() example 2

Create two js files named support.js and master.js, having the following code:

File: support.js

console.log("Child Process " + process.argv[2] + " executed." );

File: master.js

1. const fs = require('fs');
2. const child_process = require('child_process');
3. for(var i=0; i<3; i++) {
4. var workerProcess = child_process.exec('node support.js '+i,
5. function (error, stdout, stderr) {
6. if (error) {
7. console.log(error.stack);
8. console.log('Error code: '+error.code);
9. console.log('Signal received: '+error.signal);
10. }
11. console.log('stdout: ' + stdout);
12. console.log('stderr: ' + stderr);
13. });
14. workerProcess.on('exit', function (code) {
15. console.log('Child process exited with exit code '+code);
16. });
17. }

Open Node.js command prompt and run the following code:

node master.js
What is a Module in Node.js?
Consider modules to be the same as JavaScript libraries.

A set of functions you want to include in your application.

Built-in Modules
Node.js has a set of built-in modules which you can use without any further installation.

Look at our Built-in Modules Reference for a complete list of modules.

Include Modules
To include a module, use the require() function with the name of the module:

var http = require('http');

Now your application has access to the HTTP module, and is able to create a server:

http.createServer(function (req, res) {


res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);

Create Your Own Modules


You can create your own modules, and easily include them in your applications.

The following example creates a module that returns a date and time object:

Example

Create a module that returns the current date and time:


exports.myDateTime = function () {
return Date();
};

Use the exports keyword to make properties and methods available outside the module file.

Save the code above in a file called "myfirstmodule.js"

Include Your Own Module


Now you can include and use the module in any of your Node.js files.

Example

Use the module "myfirstmodule" in a Node.js file:

var http = require('http');


var dt = require('./myfirstmodule');

http.createServer(function (req, res) {


res.writeHead(200, {'Content-Type': 'text/html'});
res.write("The date and time are currently: " + dt.myDateTime());
res.end();
}).listen(8080);

output:

Notice that we use ./ to locate the module, that means that the module is located in the same folder as the Node.js file.

Save the code above in a file called "demo_module.js", and initiate the file:

Initiate demo_module.js:

C:\Users\Your Name>node demo_module.js

If you have followed the same steps on your computer, you will see the same result as the example: http://localhost:8080

The Built-in HTTP Module


Node.js has a built-in module called HTTP, which allows Node.js to transfer data over the Hyper Text Transfer Protocol
(HTTP).

To include the HTTP module, use the require() method:

var http = require('http');


Node.js as a Web Server
The HTTP module can create an HTTP server that listens to server ports and gives a response back to the client.

Use the createServer() method to create an HTTP server:

Example
var http = require('http');

//create a server object:


http.createServer(function (req, res) {
res.write('Hello World!'); //write a response to the client
res.end(); //end the response
}).listen(8080); //the server object listens on port 8080

The function passed into the http.createServer() method, will be executed when someone tries to access the computer
on port 8080.

Save the code above in a file called "demo_http.js", and initiate the file:

Initiate demo_http.js:

C:\Users\Your Name>node demo_http.js

If you have followed the same steps on your computer, you will see the same result as the example: http://localhost:8080

Add an HTTP Header


If the response from the HTTP server is supposed to be displayed as HTML, you should include an HTTP header with the
correct content type:

Example
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('Hello World!');
res.end();
}).listen(8080);

The first argument of the res.writeHead() method is the status code, 200 means that all is OK, the second argument is
an object containing the response headers.

Read the Query String


The function passed into the http.createServer() has a req argument that represents the request from the client, as an
object (http.IncomingMessage object).

This object has a property called "url" which holds the part of the url that comes after the domain name:

demo_http_url.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(req.url);
res.end();
}).listen(8080);

Save the code above in a file called "demo_http_url.js" and initiate the file:

Initiate demo_http_url.js:

C:\Users\Your Name>node demo_http_url.js

If you have followed the same steps on your computer, you should see two different results when opening these two
addresses:

http://localhost:8080/summer

Will produce this result:

/summer

http://localhost:8080/winter

Will produce this result:

/winter

Split the Query String


There are built-in modules to easily split the query string into readable parts, such as the URL module.

Example

Split the query string into readable parts:

var http = require('http');


var url = require('url');

http.createServer(function (req, res) {


res.writeHead(200, {'Content-Type': 'text/html'});
var q = url.parse(req.url, true).query;
var txt = q.year + " " + q.month;
res.end(txt);
}).listen(8080);

Save the code above in a file called "demo_querystring.js" and initiate the file:

Initiate demo_querystring.js:
C:\Users\Your Name>node demo_querystring.js

The address:

http://localhost:8080/?year=2017&month=July

Will produce this result:

2017 July

Node.js File System Module


Node.js as a File Server
The Node.js file system module allows you to work with the file system on your computer.

To include the File System module, use the require() method:

var fs = require('fs');

Common use for the File System module:

 Read files
 Create files
 Update files
 Delete files
 Rename files

Read Files
The fs.readFile() method is used to read files on your computer.

Assume we have the following HTML file (located in the same folder as Node.js):

demofile1.html

<html>
<body>
<h1>My Header</h1>
<p>My paragraph.</p>
</body>
</html>

Create a Node.js file that reads the HTML file, and return the content:

ExampleGet your own Node.js Server


var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
fs.readFile('demofile1.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
}).listen(8080);

Save the code above in a file called "demo_readfile.js", and initiate the file:

Initiate demo_readfile.js:

C:\Users\Your Name>node demo_readfile.js

If you have followed the same steps on your computer, you will see the same result as the example: http://localhost:8080

ADVERTISEMENT

Create Files
The File System module has methods for creating new files:

 fs.appendFile()
 fs.open()
 fs.writeFile()

The fs.appendFile() method appends specified content to a file. If the file does not exist, the file will be created:

Example

Create a new file using the appendFile() method:

var fs = require('fs');

fs.appendFile('mynewfile1.txt', 'Hello content!', function (err) {


if (err) throw err;
console.log('Saved!');
});

The fs.open() method takes a "flag" as the second argument, if the flag is "w" for "writing", the specified file is opened for
writing. If the file does not exist, an empty file is created:

Example

Create a new, empty file using the open() method:

var fs = require('fs');

fs.open('mynewfile2.txt', 'w', function (err, file) {


if (err) throw err;
console.log('Saved!');
});
The fs.writeFile() method replaces the specified file and content if it exists. If the file does not exist, a new file,
containing the specified content, will be created:

Example

Create a new file using the writeFile() method:

var fs = require('fs');

fs.writeFile('mynewfile3.txt', 'Hello content!', function (err) {


if (err) throw err;
console.log('Saved!');
});

Update Files
The File System module has methods for updating files:

 fs.appendFile()
 fs.writeFile()

The fs.appendFile() method appends the specified content at the end of the specified file:

Example

Append "This is my text." to the end of the file "mynewfile1.txt":

var fs = require('fs');

fs.appendFile('mynewfile1.txt', ' This is my text.', function (err) {


if (err) throw err;
console.log('Updated!');
});

The fs.writeFile() method replaces the specified file and content:

Example

Replace the content of the file "mynewfile3.txt":

var fs = require('fs');

fs.writeFile('mynewfile3.txt', 'This is my text', function (err) {


if (err) throw err;
console.log('Replaced!');
});

Delete Files
To delete a file with the File System module, use the fs.unlink() method.
The fs.unlink() method deletes the specified file:

Example

Delete "mynewfile2.txt":

var fs = require('fs');

fs.unlink('mynewfile2.txt', function (err) {


if (err) throw err;
console.log('File deleted!');
});

Rename Files
To rename a file with the File System module, use the fs.rename() method.

The fs.rename() method renames the specified file:

Example

Rename "mynewfile1.txt" to "myrenamedfile.txt":

var fs = require('fs');

fs.rename('mynewfile1.txt', 'myrenamedfile.txt', function (err) {


if (err) throw err;
console.log('File Renamed!');
});

Upload Files
You can also use Node.js to upload files to your computer

Node.js URL Module


The Built-in URL Module
The URL module splits up a web address into readable parts.

To include the URL module, use the require() method:

var url = require('url');

Parse an address with the url.parse() method, and it will return a URL object with each part of the address as properties:
ExampleGet your own Node.js Server

Split a web address into readable parts:

var url = require('url');


var adr = 'http://localhost:8080/default.htm?year=2017&month=february';
var q = url.parse(adr, true);

console.log(q.host); //returns 'localhost:8080'


console.log(q.pathname); //returns '/default.htm'
console.log(q.search); //returns '?year=2017&month=february'

var qdata = q.query; //returns an object: { year: 2017, month: 'february' }


console.log(qdata.month); //returns 'february'

Node.js File Server


Now we know how to parse the query string, and in the previous chapter we learned how to make Node.js behave as a file
server. Let us combine the two, and serve the file requested by the client.

Create two html files and save them in the same folder as your node.js files.

summer.html

<!DOCTYPE html>
<html>
<body>
<h1>Summer</h1>
<p>I love the sun!</p>
</body>
</html>

winter.html

<!DOCTYPE html>
<html>
<body>
<h1>Winter</h1>
<p>I love the snow!</p>
</body>
</html>

Create a Node.js file that opens the requested file and returns the content to the client. If anything goes wrong, throw a
404 error:

demo_fileserver.js:

var http = require('http');


var url = require('url');
var fs = require('fs');

http.createServer(function (req, res) {


var q = url.parse(req.url, true);
var filename = "." + q.pathname;
fs.readFile(filename, function(err, data) {
if (err) {
res.writeHead(404, {'Content-Type': 'text/html'});
return res.end("404 Not Found");
}
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
}).listen(8080);

Remember to initiate the file:

Initiate demo_fileserver.js:

C:\Users\Your Name>node demo_fileserver.js

If you have followed the same steps on your computer, you should see two different results when opening these two
addresses:

http://localhost:8080/summer.html

Will produce this result:

Summer
I love the sun!

http://localhost:8080/winter.html

Will produce this result:

Winter
I love the snow!

Express.js: Introduction to Express Framework

Express.js is a web framework for Node.js. It is a fast, robust and asynchronous in nature.

Our Express.js tutorial includes all topics of Express.js such as Express.js installation on windows and linux, request object, response
object, get method, post method, cookie management, scaffolding, file upload, template etc.

What is Express.js

Express is a fast, assertive, essential and moderate web framework of Node.js. You can assume express as a layer built on the top of
the Node.js that helps manage a server and routes. It provides a robust set of features to develop web and mobile applications.

Let's see some of the core features of Express framework:

o It can be used to design single-page, multi-page and hybrid web applications.


o It allows to setup middlewares to respond to HTTP Requests.
o It defines a routing table which is used to perform different actions based on HTTP method and URL.
o It allows to dynamically render HTML Pages based on passing arguments to templates.

Why use Express

o Ultra fast I/O


o Asynchronous and single threaded
o MVC like structure
o Robust API makes routing easy

How does Express look like

Let's see a basic Express.js app.

File: basic_express.js

var express = require('express');

var app = express();

app.get('/', function (req, res) {

res.send('Welcome to JavaTpoint!');

});

var server = app.listen(8000, function () {

var host = server.address().address;

var port = server.address().port;

console.log('Example app listening at http://%s:%s', host, port);

});
Output:

Getting Started with Express

What is Express.js?
Express is a lightweight and versatile Node.js web application framework that provides various features for mobile and web apps.

We can quickly build server-side web apps and APIs with Express.js. Even front-end devs who know JavaScript but have no
expertise with the back-end may learn and create APIs with Node.js and Express.js.

Let’s now learn how to create a simple Todos API with Node.js + Express.js.

Installation and Setup


To install Express.js on a project, we will use the following commands,

# Initialize NPM
npm init -y

# Install Express.js
npm install express
Code language: Bash (bash)

To verify the installation, we check if there is a package.json file created and if express gets listed as a dependency.
After installation, create an index.js file at the root of the project. This file is the entry point of our Express.js application.
We first create an app instance using the express function. Furthermore, we define a port and will start listening to it using
the app.listen() method.

On running our file using node index.js, we see a console message ‘server running.’
It means that we have successfully created an Express.js server. We can’t see anything on navigating to the URL because we
haven’t set up any endpoints yet.

Routing
Routing is the technique by which a web server replies to a client request to a certain endpoint. The endpoint comprises a URI (a
path like / or /todos) and an HTTP method like GET, POST, PUT, DELETE, etc.

To define a route in Express.js, we write,

app.METHOD(PATH, HANDLER);
Code language: JavaScript (javascript)

The METHOD is a valid HTTP request method, and PATH is the path to our endpoint (like / or /todos ). HANDLER is a callback
function that runs whenever a request to that endpoint is received.

Routing parameters
Route parameters store the value of URL segments at the position where they appear. Since the URL contains parameters, we use
the request.params object to access them.

app.get('/todos/:id', (request, response) => {


// do something
})
Code language: JavaScript (javascript)

On making a GET request at /todos/123, the request.params.id will correspond to ‘123’.

Request and Response


The client submits a request to a server, and the server responds to the client. A request and a response object are supplied as
arguments whenever the callback function (or route handler function) associated with an endpoint gets invoked.

This request object contains all the data about the request, like the path, the headers, the request payload, etc. On the other
hand, the response object has methods to modify and send the response back to the respective client.

HTTP methods recap


There are five basic HTTP methods accessible. Let’s take a look at them one by one.

GET

The GET method requests some information from the server.

app.get(PATH, (request, response) => {


// send information
})
Code language: JavaScript (javascript)
POST

The POST method sends information to the server, which causes a change in the server’s state.

app.post(PATH, (request, response) => {


// add information
})
Code language: JavaScript (javascript)
PUT

The PUT method replaces previously existing information on the server with updated information.

app.put(PATH, (request, response) => {


// update information
})
Code language: JavaScript (javascript)
PATCH

The PATCH method makes partial changes to existing information on the server.

app.patch(PATH, (request, response) => {


// patch information
})
Code language: JavaScript (javascript)
DELETE

The DELETE method deletes some specified information from the server.

app.delete(PATH, (request, response) => {


// delete information
})
Code language: JavaScript (javascript)

Setting up an endpoint
As we have already seen, creating an endpoint is a simple task. Let’s create a basic endpoint that responds with the text ‘Hello
from Codedamn.’

app.get('/hi', (request, response) => {


response.send('Hello from Codedam');
})
Code language: JavaScript (javascript)

Let’s break down the code piece by piece.

get
It is the HTTP request method.

/hi
The string hi represents the path to our endpoint.

(req, res) => { ... }


It is the request handler. It’s a callback function that gets called whenever the client makes a GET request to /hi .
On navigating to the endpoint, we get the response “Hello from Codedam.”

Your first Express App

Step by step Implementation:


Step 1: Write this command in your terminal, to create a nodejs application, because our express server will work inside
the node application.
Syntax:
npm init
This will ask you for few configurations about your project you can fill them accordingly, also you can change it later from
the package.json file.
Note: Use `npm init -y` for default initialization

Step 2: Install necessary dependencies for our application.


npm install express
Something like this will be shown on successful installation,
Step 3: The project structure will look like following.

Create a file app.js, for this article, we will write the whole express code in that file. This will be our folder structure. Now
Inside app.js, Import express with require keyword and create an app by calling the express() function provided by the
express framework. Set the port for our local application, 3000 is the default but you can choose any according to the
availability of ports. Call the listen() function, It requires path and callback as an argument. It starts listening to the
connection on the specified path, the default host is localhost, and our default path for the local machine is
the localhost:3000, here 3000 is the port which we have set earlier. The callback function gets executed either on the
successful start of the server or due to an error.

const express = require('express');


const app = express();

const PORT = 3000;

app.listen(PORT, (error) =>{

if(!error)

console.log("Server is Successfully Running,

and App is listening on port "+ PORT)

else

console.log("Error occurred, server can't start", error);

);

Step to run the application: Now as we have created a server we can successfully start running it to see it’s working,
write this command in your terminal to start the express server.
node app.js
Output: You will see something like this on the terminal.

Express Routing

Express.js Routing
Routing is made from the word route. It is used to determine the specific behavior of an application. It specifies how an application
responds to a client request to a particular route, URI or path and a specific HTTP request method (GET, POST, etc.). It can handle
different types of HTTP requests.

Let's take an example to see basic routing.

File: routing_example.js

1. var express = require('express');


2. var app = express();
3. app.get('/', function (req, res) {
4. console.log("Got a GET request for the homepage");
5. res.send('Welcome to JavaTpoint!');
6. })
7. app.post('/', function (req, res) {
8. console.log("Got a POST request for the homepage");
9. res.send('I am Impossible! ');
10. })
11. app.delete('/del_student', function (req, res) {
12. console.log("Got a DELETE request for /del_student");
13. res.send('I am Deleted!');
14. })
15. app.get('/enrolled_student', function (req, res) {
16. console.log("Got a GET request for /enrolled_student");
17. res.send('I am an enrolled student.');
18. })
19. // This responds a GET request for abcd, abxcd, ab123cd, and so on
20. app.get('/ab*cd', function(req, res) {
21. console.log("Got a GET request for /ab*cd");
22. res.send('Pattern Matched.');
23. })
24. var server = app.listen(8000, function () {
25. var host = server.address().address
26. var port = server.address().port
27. console.log("Example app listening at http://%s:%s", host, port)
28. })

Now, you can see the result generated by server on the local host http://127.0.0.1:8000

Output:

This is the homepage of the example app


Implementing MVC in Express

What is the MVC?

MVC stands for Model, View, Controller is an architectural pattern that separates an application into three
main logical components: the model, the view, and the controller. Each one of these components is built to
handle specific development aspects of an application.
MVC is architecture used in building Web Servers that focus on reliability and making the development
process much simpler and easier since it composes the Web Server into three separate parts.

 Controller is the part that takes care of client request processing which handles the HTTP
Request and returns a response the response could be either a JSON if you’re calling an API
endpoint or regular HTML webpage.

 Model is the database interface which lets you interact with the database API and create different
entity schemas of your app on the database (MySQL, MongoDB), it gets called from controller
depending on the client’s request if it needs a stored data then the controller will ask the model
interface for providing it with the needed data.

 View is what compiles and renders into plain HTML and what the client in most cases going to get
back as a response of what he requested (for ex: to view his profile details), the view needs to use a
Template Engine for doing the rendering process where the controller feed it with needed data
(data from database and client) and the view renders and convert everything into plain HTML that
the browser could understand and display.

So the three different components interact with each other perfectly to ensure reliable and flexible client
request processing, either for simple tasks like accessing the home page or updating his profile data, that’s
why building your Web Server around and MVC Architecture is really cool and will let you with tons of
options and flexible thoughts.

You may also wanna take a closer look on the Diagram I made for the video to explain the MVC
architecture and how it works with a client.
Create an MVC Express App

Now let’s try to make things clear about why using an MVC is useful and how it can turn your standard
Express/Node.js App into a high-level app just by wrapping around an MVC Architecture.

You can clone the basic Express App we will try to work on from this Github Repo.

The repository contains a very basic Node.js/Express app that has only one route for the home page and
some pug (Template Engine) templates, let’s try to create a very simple Restaurant Menu (Meals) App.

Now, since the Architecture is composed of three main parts (Controller, Model and View) so under the
src/ folder go and create a new subfolder for each MVC part.
src/
--controllers/
--models/
--views/
--routes/
boostrap.js

Also, make sure to create a routes folder for holding all the routes that our app going to have, since a
controller must be linked with route to listen for a specific HTTP request that comes across this route, and
a bootstrap.js file that will do the job of starting the application and liking all the three components
together.

The app.js module on the root folder is the main server start point has the setup for a minimal Express
Server with Body parser and a simple home page route handler.

Middleware

Middleware are different types of functions that are invoked by the Express.js routing layer before the final request handler . As the
name specified, Middleware appears in the middle between an initial request and final intended route. In stack, middleware
functions are always invoked in the order in which they are added.

Middleware is commonly used to perform tasks like body parsing for URL-encoded or JSON requests, cookie parsing for basic
cookie handling, or even building JavaScript modules on the fly.

What is a Middleware function

Middleware functions are the functions that access to the request and response object (req, res) in request-response cycle.

A middleware function can perform the following tasks:


o It can execute any code.
o It can make changes to the request and the response objects.
o It can end the request-response cycle.
o It can call the next middleware function in the stack.

Express.js Middleware

Following is a list of possibly used middleware in Express.js app:

o Application-level middleware
o Router-level middleware
o Error-handling middleware
o Built-in middleware
o Third-party middleware

Let's take an example to understand what middleware is and how it works.

Let's take the most basic Express.js app:

File: simple_express.js

1. var express = require('express');


2. var app = express();
3.
4. app.get('/', function(req, res) {
5. res.send('Welcome to JavaTpoint!');
6. });
7. app.get('/help', function(req, res) {
8. res.send('How can I help You?');
9. });
10. var server = app.listen(8000, function () {
11. var host = server.address().address
12. var port = server.address().port
13. console.log("Example app listening at http://%s:%s", host, port)
14. })
Using Template Engines

What is a template engine


A template engine facilitates you to use static template files in your applications. At
runtime, it replaces variables in a template file with actual values, and transforms the
template into an HTML file sent to the client. So this approach is preferred to design
HTML pages easily.

Following is a list of some popular template engines that work with Express.js:

o Pug (formerly known as jade)


o mustache
o dust
o atpl
o eco
o ect
o ejs
o haml
o haml-coffee
o handlebars
o hogan
o jazz
o jqtpl
o JUST
o liquor
o QEJS
o swig
o templayed
o toffee
o underscore
o walrus
o whiskers

In the above template engines, pug (formerly known as jade) and mustache seems to be
most popular choice. Pug is similar to Haml which uses whitespace. According to the
template-benchmark, pug is 2x slower than Handlebars, EJS, Underscore. ECT seems to
be the fastest. Many programmers like mustache template engine mostly because it is
one of the simplest and versatile template engines.

Using template engines with Express


Template engine makes you able to use static template files in your application. To
render template files you have to set the following application setting properties:

o Views: It specifies a directory where the template files are located.

For example: app.set('views', './views').

o view engine: It specifies the template engine that you use. For example, to use the Pug template engine: app.set('view
engine', 'pug').
Let's take a template engine pug (formerly known as jade).

Pug Template Engine

Let's learn how to use pug template engine in Node.js application using Express.js. Pug is a template engine for Node.js. Pug uses
whitespaces and indentation as the part of the syntax. Its syntax is aesy to learn.

Install pug

Execute the following command to install pug template engine:

npm install pug --save

Pug template must be written inside .pug file and all .pug files must be put inside views folder in the root folder of Node.js
application.

Note: By default Express.js searches all the views in the views folder under the root folder. you can also set to another folder using
views property in express. For example: app.set('views','MyViews').

The pug template engine takes the input in a simple way and produces the output in HTML. See how it renders HTML:

Simple input:

1. doctype html
2. html
3. head
4. title A simple pug example
5. body
6. h1 This page is produced by pug template engine
7. p some paragraph here..
Output produced by pug template:

1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>A simple pug example</title>
5. </head>
6. <body>
7. <h1>This page is produced by pug template engine</h1>
8. <p>some paragraph here..</p>
9. </body>
10. </html>

Express.js can be used with any template engine. Let's take an example to deploy how pug template creates HTML page dynamically.

See this example:

Create a file named index.pug file inside views folder and write the following pug template in it:

1. doctype html
2. html
3. head
4. title A simple pug example
5. body
6. h1 This page is produced by pug template engine
7. p some paragraph here..

File: server.js

1. var express = require('express');


2. var app = express();
3. //set view engine
4. app.set("view engine","pug")
5. app.get('/', function (req, res) {
6. res.render('view.pug', index);
7. res.render('index');
8. });
9. var server = app.listen(5000, function () {
10. console.log('Node server is running..');
11. });

Error Handling

Error handling in Express is done using middleware. But this middleware has special properties. The error handling middleware are
defined in the same way as other middleware functions, except that error-handling functions MUST have four arguments instead of
three – err, req, res, next. For example, to send a response on any error, we can use −

app.use(function(err, req, res, next) {


console.error(err.stack);
res.status(500).send('Something broke!');
});

Till now we were handling errors in the routes itself. The error handling middleware allows us to separate our error logic an d send
responses accordingly. The next() method we discussed in middleware takes us to next middleware/route handler.

For error handling, we have the next(err) function. A call to this function skips all middleware and matches us to the next error
handler for that route. Let us understand this through an example.

var express = require('express');


var app = express();

app.get('/', function(req, res){


//Create an error and pass it to the next function
var err = new Error("Something went wrong");
next(err);
});

/*
* other route handlers and middleware here
* ....
*/

//An error handling middleware


app.use(function(err, req, res, next) {
res.status(500);
res.send("Oops, something went wrong.")
});

app.listen(3000);

This error handling middleware can be strategically placed after routes or contain conditions to detect error types and respond to the
clients accordingly. The above program will display the following output.

API Handling

An API is always needed to create mobile applications, single page applications, use AJAX calls and provide data to clients. An
popular architectural style of how to structure and name these APIs and the endpoints is called REST(Representational Transfer
State). HTTP 1.1 was designed keeping REST principles in mind. REST was introduced by Roy Fielding in 2000 in his Paper
Fielding Dissertations.

RESTful URIs and methods provide us with almost all information we need to process a request. The table given below summarizes
how the various verbs should be used and how URIs should be named. We will be creating a movies API towards the end; let us
now discuss how it will be structured.

Method URI Details Function


GET /movies Safe, Gets the list of all movies and their details
cachable

GET /movies/1234 Safe, Gets the details of Movie id 1234


cachable

POST /movies N/A Creates a new movie with the details provided.
Response contains the URI for this newly created
resource.

PUT /movies/1234 Idempotent Modifies movie id 1234(creates one if it doesn't


already exist). Response contains the URI for this
newly created resource.

DELETE /movies/1234 Idempotent Movie id 1234 should be deleted, if it exists.


Response should contain the status of the request.

DELETE /movies Invalid Should be invalid. DELETE and PUT should


or PUT specify which resource they are working on.

Let us now create this API in Express. We will be using JSON as our transport data format as it is easy to work with in JavaScript
and has other benefits. Replace your index.js file with the movies.js file as in the following program.

index.js
var express = require('express');
var bodyParser = require('body-parser');
var multer = require('multer');
var upload = multer();

var app = express();

app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(upload.array());

//Require the Router we defined in movies.js


var movies = require('./movies.js');

//Use the Router on the sub route /movies


app.use('/movies', movies);

app.listen(3000);

Now that we have our application set up, let us concentrate on creating the API.

Start by setting up the movies.js file. We are not using a database to store the movies but are storing them in memory; so ev ery time
the server restarts, the movies added by us will vanish. This can easily be mimicked using a database or a file (using nod e fs
module).

Once you import Express then, create a Router and export it using module.exports −

var express = require('express');


var router = express.Router();
var movies = [
{id: 101, name: "Fight Club", year: 1999, rating: 8.1},
{id: 102, name: "Inception", year: 2010, rating: 8.7},
{id: 103, name: "The Dark Knight", year: 2008, rating: 9},
{id: 104, name: "12 Angry Men", year: 1957, rating: 8.9}
];

//Routes will go here


module.exports = router;

GET routes
Let us define the GET route for getting all the movies −

router.get('/', function(req, res){


res.json(movies);
});

To test out if this is working fine, run your app, then open your terminal and enter −
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET
localhost:3000/movies

The following response will be displayed −

[{"id":101,"name":"Fight Club","year":1999,"rating":8.1},
{"id":102,"name":"Inception","year":2010,"rating":8.7},
{"id":103,"name":"The Dark Knight","year":2008,"rating":9},
{"id":104,"name":"12 Angry Men","year":1957,"rating":8.9}]

We have a route to get all the movies. Let us now create a route to get a specific movie by its id.

router.get('/:id([0-9]{3,})', function(req, res){


var currMovie = movies.filter(function(movie){
if(movie.id == req.params.id){
return true;
}
});
if(currMovie.length == 1){
res.json(currMovie[0])
} else {
res.status(404);//Set status to 404 as movie was not found
res.json({message: "Not Found"});
}
});

This will get us the movies according to the id that we provided. To check the output, use the following command in your terminal −
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET
localhost:3000/movies/101

You'll get the following response −


{"id":101,"name":"Fight Club","year":1999,"rating":8.1}

If you visit an invalid route, it will produce a cannot GET error while if you visit a valid route with an id that doesn’t exist, it will
produce a 404 error.

We are done with the GET routes, let us now move on to the POST route.

POST route
Use the following route to handle the POSTed data −

router.post('/', function(req, res){


//Check if all fields are provided and are valid:
if(!req.body.name ||
!req.body.year.toString().match(/^[0-9]{4}$/g) ||
!req.body.rating.toString().match(/^[0-9]\.[0-9]$/g)){

res.status(400);
res.json({message: "Bad Request"});
} else {
var newId = movies[movies.length-1].id+1;
movies.push({
id: newId,
name: req.body.name,
year: req.body.year,
rating: req.body.rating
});
res.json({message: "New movie created.", location: "/movies/" + newId});
}
});

This will create a new movie and store it in the movies variable. To check this route, enter the following code in your terminal −

curl -X POST --data "name = Toy%20story&year = 1995&rating = 8.5" http://localhost:3000/movies

The following response will be displayed −

{"message":"New movie created.","location":"/movies/105"}

To test if this was added to the movies object, Run the get request for /movies/105 again. The following response will be displayed

{"id":105,"name":"Toy story","year":"1995","rating":"8.5"}
Let us move on to create the PUT and DELETE routes.

PUT route
The PUT route is almost the same as the POST route. We will be specifying the id for the object that'll be updated/created. C reate
the route in the following way.

router.put('/:id', function(req, res){


//Check if all fields are provided and are valid:
if(!req.body.name ||
!req.body.year.toString().match(/^[0-9]{4}$/g) ||
!req.body.rating.toString().match(/^[0-9]\.[0-9]$/g) ||
!req.params.id.toString().match(/^[0-9]{3,}$/g)){

res.status(400);
res.json({message: "Bad Request"});
} else {
//Gets us the index of movie with given id.
var updateIndex = movies.map(function(movie){
return movie.id;
}).indexOf(parseInt(req.params.id));

if(updateIndex === -1){


//Movie not found, create new
movies.push({
id: req.params.id,
name: req.body.name,
year: req.body.year,
rating: req.body.rating
});
res.json({message: "New movie created.", location: "/movies/" + req.params.id});
} else {
//Update existing movie
movies[updateIndex] = {
id: req.params.id,
name: req.body.name,
year: req.body.year,
rating: req.body.rating
};
res.json({message: "Movie id " + req.params.id + " updated.",
location: "/movies/" + req.params.id});
}
}
});

This route will perform the function specified in the above table. It will update the object with new d etails if it exists. If it doesn't exist,
it will create a new object. To check the route, use the following curl command. This will update an existing movie. To creat e a new
Movie, just change the id to a non-existing id.
curl -X PUT --data "name = Toy%20story&year = 1995&rating = 8.5"
http://localhost:3000/movies/101

Response

{"message":"Movie id 101 updated.","location":"/movies/101"}


DELETE route
Use the following code to create a delete route. −

router.delete('/:id', function(req, res){


var removeIndex = movies.map(function(movie){
return movie.id;
}).indexOf(req.params.id); //Gets us the index of movie with given id.

if(removeIndex === -1){


res.json({message: "Not found"});
} else {
movies.splice(removeIndex, 1);
res.send({message: "Movie id " + req.params.id + " removed."});
}
});

Check the route in the same way as we checked the other routes. On successful deletion(for example id 105), you will get the
following output −
{message: "Movie id 105 removed."}

Finally, our movies.js file will look like the following.

var express = require('express');


var router = express.Router();
var movies = [
{id: 101, name: "Fight Club", year: 1999, rating: 8.1},
{id: 102, name: "Inception", year: 2010, rating: 8.7},
{id: 103, name: "The Dark Knight", year: 2008, rating: 9},
{id: 104, name: "12 Angry Men", year: 1957, rating: 8.9}
];
router.get('/:id([0-9]{3,})', function(req, res){
var currMovie = movies.filter(function(movie){
if(movie.id == req.params.id){
return true;
}
});

if(currMovie.length == 1){
res.json(currMovie[0])
} else {
res.status(404); //Set status to 404 as movie was not found
res.json({message: "Not Found"});
}
});
router.post('/', function(req, res){
//Check if all fields are provided and are valid:
if(!req.body.name ||
!req.body.year.toString().match(/^[0-9]{4}$/g) ||
!req.body.rating.toString().match(/^[0-9]\.[0-9]$/g)){
res.status(400);
res.json({message: "Bad Request"});
} else {
var newId = movies[movies.length-1].id+1;
movies.push({
id: newId,
name: req.body.name,
year: req.body.year,
rating: req.body.rating
});
res.json({message: "New movie created.", location: "/movies/" + newId});
}
});

router.put('/:id', function(req, res) {


//Check if all fields are provided and are valid:
if(!req.body.name ||
!req.body.year.toString().match(/^[0-9]{4}$/g) ||
!req.body.rating.toString().match(/^[0-9]\.[0-9]$/g) ||
!req.params.id.toString().match(/^[0-9]{3,}$/g)){
res.status(400);
res.json({message: "Bad Request"});
} else {
//Gets us the index of movie with given id.
var updateIndex = movies.map(function(movie){
return movie.id;
}).indexOf(parseInt(req.params.id));

if(updateIndex === -1){


//Movie not found, create new
movies.push({
id: req.params.id,
name: req.body.name,
year: req.body.year,
rating: req.body.rating
});
res.json({
message: "New movie created.", location: "/movies/" + req.params.id});
} else {
//Update existing movie
movies[updateIndex] = {
id: req.params.id,
name: req.body.name,
year: req.body.year,
rating: req.body.rating
};
res.json({message: "Movie id " + req.params.id + " updated.",
location: "/movies/" + req.params.id});
}
}
});

router.delete('/:id', function(req, res){


var removeIndex = movies.map(function(movie){
return movie.id;
}).indexOf(req.params.id); //Gets us the index of movie with given id.

if(removeIndex === -1){


res.json({message: "Not found"});
} else {
movies.splice(removeIndex, 1);
res.send({message: "Movie id " + req.params.id + " removed."});
}
});
module.exports = router;

This completes our REST API. Now you can create much more complex applications using this simple architectural style and
Express.

Using Process Managers

When you run Express apps for production, it is helpful to use a process manager to:

 Restart the app automatically if it crashes.


 Gain insights into runtime performance and resource consumption.
 Modify settings dynamically to improve performance.
 Control clustering.

A process manager is somewhat like an application server: it’s a “container” for applications that facilitates deployment, provides high availability, and

enables you to manage the application at runtime.

The most popular process managers for Express and other Node.js applications are:

 Forever: A simple command-line interface tool to ensure that a script runs continuously (forever). Forever’s simple interface makes it ideal
for running smaller deployments of Node.js apps and scripts.
 PM2: A production process manager for Node.js applications that has a built-in load balancer. PM2 enables you to keep applications alive
forever, reloads them without downtime, helps you to manage application logging, monitoring, and clustering.
 StrongLoop Process Manager (Strong-PM): A production process manager for Node.js applications with built-in load balancing,
monitoring, and multi-host deployment. Includes a CLI to build, package, and deploy Node.js applications to a local or remote system.
 SystemD: The default process manager on modern Linux distributions, that makes it simple to run a Node application as a service

You might also like