PHP Programming
PHP Programming
Ch04
Express Framework
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.
response.
Installing Express
• Firstly, install the Express framework globally using
NPM so that it can be used to create a web application
using node terminal.
npm install express –save
npm install express
• The above command saves the installation locally in
the node_modules directory and creates a directory
express inside node_modules.
Usage
• Express module is factory for application
var app = expresss( );
• Application have many methods for configuring
• Are also function that can be given to standard node
http and https modules
• Invoke app.listen() method after configuring or give
to http or https modules
• App.listen (3000, function (req, res){
• Console.log(express ready for port 3000’);
• });
Objects of Express
• There are four main objects of Express :
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.
• HANDLER is the function executed when the route is
matched.
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
Express.
• express.static(root, [options])
• app.use(express.static('public'))
Serving static files
• Express provides a built-in middleware express.static to serve static files, such
• You simply need to pass the name of the directory where you keep your static
• For example, if you keep your images, CSS, and JavaScript files in a directory
• NPM helps you install the various modules you need for
your web development and not just given you a whole
bunch of features you might never need
Most important Packages
• Nodemone : is a tool that helps develop Node. js
based applications by automatically restarting the
node application when file changes in the directory
are detected.
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')
• })
Basic Routing
• We have seen a basic application which serves
HTTP request for the homepage. 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.
var mailOptions = {
from: 'youremail@gmail.com',
to: 'myfriend@yahoo.com',
subject: 'Sending Email using Node.js',
text: 'That was easy!'
};