Chapter Four
Chapter Four
Ch04
Framework Express
1
Express Framework
• Express is a node js web application framework that
provides broad features for building web and mobile
applications. It is used to build a single page,
multipage, and hybrid web application.
Application (app)
Request (req)
Respond (res)
Router (express.router)
Routing
• Routing refers to determining how an
application responds to a client request to a
particular endpoint, which is a URI (or path) and a
specific HTTP request method (GET, POST, and so
on).
• Each route can have one or more handler
functions, which are executed when the route is
matched.
• Route definition takes the following structure:
app.METHOD(PATH, HANDLER)
• app is an instance of express.
• METHOD is an HTTP request method, in
lowercase.
• PATH is a path on the server.
Example
app.get('/', (req, res) => {
res.send('Hello World!')
})
• App.redirector ( )
Middleware
• 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.
elements of a
middleware
• The following figure shows the elements
of a middleware function call:
Example Using Next () Middle
Ware
• Using next(): If you have any middleware
function and below the next() you have some
lines or function that you want to execute, then
by using next() you can actually execute the lines
or function because it runs the code
below next() after all middleware function
finished.
Example of Next()
Middleware
• const express = require('express')
• const app = express();
•
app.use((req, res, next)=>{
• console.log('first middle ware')
• next();
• })
• express.static(root, [options])
• app.use(express.static('public'))
Serving static files
• Express provides a built-in middleware express.static to serve static
• You simply need to pass the name of the directory where you keep
• For example, if you keep your images, CSS, and JavaScript files in a
('public'));
NPM
• NPM : it is an online repository for the publishing
of open-source Node. js projects
package.json file.
Example Express
• const express = require('express')
• const app = express();
• EJS (Embedded
JavaScript Templating) is one of the
most popular template engines for
JavaScript. As the name suggests, it lets
us embed JavaScript code in a template
language that is then used to generate
HTML.
app.get('/about',(req , res)=>{
• res.render('about')
• });
app.get('/register',(req ,res)=>{
• res.render('register')
• })
•
app.use((req ,res)=>{
• res.status(404).render('404')
• })
Nodemailer Module
• Nodemailer is a Node. js module that
allows you to send emails from your
server with ease. Whether you want to
communicate with your users or just
notify yourself when something has gone
wrong, one of the options for doing so is
through mail.
Example of
NodeMailer Module
• npm install nodemailer
• var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'yourpassword'
}
});
var mailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: 'Sending Email using Node.js',
text: 'That was easy!'
};