UNIT III r20 iot
UNIT III r20 iot
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.
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.
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');
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
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.
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.
The process is a global object, an instance of EventEmitter, can be accessed from anywhere.
Property Description
platform returns platform of the process: 'darwin', 'freebsd', 'linux', 'sunos' or 'win32'
Let's see the simple process example to print architecture, pid, platform and version of the process.
File: process_example1.js
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
node process_example2.js
Function Description
Let's see the process example to print current working directory and uptime of the process.
File: process_example3.js
node process_example3.js
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.
The child_process.exec() method runs a command in a console and buffers the output.
Syntax:
Parameters:
callback: The callback function specifies three arguments error, stdout and stderr which is called with the following output when
process terminates.
Let's see the simple process example to print architecture, pid, platform and version of the process.
File: child_process_example1.js
File: my.bat
1. dir
2. mkdir child
Create two js files named support.js and master.js, having the following code:
File: support.js
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. }
node master.js
What is a Module in Node.js?
Consider modules to be the same as JavaScript libraries.
Built-in Modules
Node.js has a set of built-in modules which you can use without any further installation.
Include Modules
To include a module, use the require() function with the name of the module:
Now your application has access to the HTTP module, and is able to create a server:
The following example creates a module that returns a date and time object:
Example
Use the exports keyword to make properties and methods available outside the module file.
Example
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:
If you have followed the same steps on your computer, you will see the same result as the example: http://localhost:8080
Example
var http = require('http');
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:
If you have followed the same steps on your computer, you will see the same result as the example: http://localhost:8080
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.
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:
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
/summer
http://localhost:8080/winter
/winter
Example
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
2017 July
var fs = require('fs');
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:
Save the code above in a file called "demo_readfile.js", and initiate the file:
Initiate 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
var fs = require('fs');
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
var fs = require('fs');
Example
var fs = require('fs');
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
var fs = require('fs');
Example
var fs = require('fs');
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');
Rename Files
To rename a file with the File System module, use the fs.rename() method.
Example
var fs = require('fs');
Upload Files
You can also use Node.js to upload files to your computer
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
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:
Initiate 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
Summer
I love the sun!
http://localhost:8080/winter.html
Winter
I love the snow!
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.
File: basic_express.js
res.send('Welcome to JavaTpoint!');
});
});
Output:
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.
# 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.
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.
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.
GET
The POST method sends information to the server, which causes a change in the server’s state.
The PUT method replaces previously existing information on the server with updated information.
The PATCH method makes partial changes to existing information on the server.
The DELETE method deletes some specified information from the server.
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.’
get
It is the HTTP request method.
/hi
The string hi represents the path to our endpoint.
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.
if(!error)
else
);
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.
File: routing_example.js
Now, you can see the result generated by server on the local host http://127.0.0.1:8000
Output:
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.
Middleware functions are the functions that access to the request and response object (req, res) in request-response cycle.
Express.js Middleware
o Application-level middleware
o Router-level middleware
o Error-handling middleware
o Built-in middleware
o Third-party middleware
File: simple_express.js
Following is a list of some popular template engines that work with Express.js:
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.
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).
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
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.
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
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 −
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.
/*
* other route handlers and middleware here
* ....
*/
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.
POST /movies N/A Creates a new movie with the details provided.
Response contains the URI for this newly created
resource.
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();
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(upload.array());
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 −
GET routes
Let us define the GET route for getting all the 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
[{"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.
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
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 −
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 −
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.
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));
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
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."}
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});
}
});
This completes our REST API. Now you can create much more complex applications using this simple architectural style and
Express.
When you run Express apps for production, it is helpful to use a process manager to:
A process manager is somewhat like an application server: it’s a “container” for applications that facilitates deployment, provides high availability, and
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