Node - Js - Express Framework
Node - Js - Express Framework
js - Express
Framework
Introduction
Express is a relatively small framework that sits on top of
Node.js’s web server functionality to simplify its APIs and
add helpful new features. It makes it easier to organize your
application’s functionality with middleware and routing; it
adds helpful utilities to Node.js’s HTTP objects; it facilitates
the rendering of dynamic HTML views; it defines an easily
implemented extensibility standard.
It is a fast, robust and asynchronous in nature.
It can be used to design single-page, multi-page and hybrid
web applications.
It allows to setup middlewares to respond to HTTP
Requests.
It defines a routing table which is used to perform different
actions based on HTTP method and URL.
It allows to dynamically render HTML Pages based on
passing arguments to templates.
Installing Express
The above commands saves the installation
locally in the node_modules directory and
creates a directory express inside
node_modules. You should install the following
important modules along with express −
body-parser − This is a node.js middleware for
handling JSON, Raw, Text and URL encoded form
data.
cookie-parser − Parse Cookie header and
populate req.cookies with an object keyed by
the cookie names. Note: No need to
install npm
multer − This is a node.js middleware for
packages, already
available in
handling multipart/form-data. node_modules
Writing middleware for use in
Express apps
Middleware functions are functions that have access to
the request object (req), the response object (res), and
the next() function in the application’s request-response
cycle. The next() function is a function in the Express
router which, when invoked, executes the middleware
succeeding the current middleware.
Middleware functions can perform the following tasks:
Execute any code.
Make changes to the request and the response objects.
End the request-response cycle.
Call the next middleware in the stack.
If the current middleware function does not end the
request-response cycle, it must call next() to pass control
to the next middleware function. Otherwise, the request
will be left hanging
The following figure shows the
elements of a middleware
function call:
Example with middleware
Here is an example of a simple “Hello World”
Express application.
index.html
get_ex1.js
post_ex1.js
Data Validation
Express itself doesn't provide any specific
support for form handling operations, but it
can use middleware to
process POST and GET parameters from the
form, and to validate their values.
import the express-validator module after
the other modules.
checkBody(parameter, message): Specifies a body
(POST) parameter to validate along with a message to
be displayed if it fails the tests. The validation criteria
are daisy chained to the checkBody() method. For
example, the first check below will test that the
"name" parameter is alphanumeric and set an error
message "Invalid name" if it is not. The second test
checks that the age parameter has an integer value.
validation_ex.js
Express.js Routing
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.
◦ http://localhost:3000/Books
◦ http://localhost:3000/Students
In the above example, If a GET request is made for the first
URL, then the response should ideally be a list of books. If the
GET request is made for the second URL, then the response
should ideally be a list of Students.
So based on the URL which is accessed, a different
functionality on the web server will be invoked and accordingly
the response will be sent to the client. This is the concept of
routing.
Each route can have one or more handler functions, which are
executed when the route is matched.
Configure Routes:
index.html
route_ex.js
Access MongoDB in
Node.js
Introduction to MongoDB
MongoDB is a No SQL database. It is
an open-source, cross-platform,
document-oriented database written in
C++.
Mongo DB is developed and supported
by a company named 10gen.
Main purpose to build MongoDB:
◦ Scalability
◦ Performance
◦ High Availability
◦ Scaling from single server deployments to large,
complex multi-site architectures.
Installation
Node.js has the ability to work with both
MySQL and MongoDB as databases. In order to
use either of these databases, you need to
download and use the required modules using
the Node package manager.
We first have to install MongoDB on Ubuntu
System by the following command
sudo apt-get install -y mongodb
To start mongo db shell type
mongo
Next we have to install MongoDB driver for
node.js through npm, if we want to use it as
our database.
MongoDB Shell
MongoDB have a JavaScript shell that
allows interaction with MongoDB
instance from the command line.
To start the shell, open command
prompt, run it as a administrator then
run the mongo executable:
disp.ejs
Display in JSON format
Search module
search.html