CP 323Lecture 4
CP 323Lecture 4
Using JavaScript
Express Framework
Mean Stack Development
● MEAN, a free, open-source, full-stack
solution for MEAN applications.
● MEAN combines MongoDB, Express.js,
AngularJS, and Node.js into a single, full
stack solution for JavaScript development.
Express Framework
● Express is a minimal and flexible Node.js web
application framework that provides a robust
set of features to develop web and mobile
applications.
● It facilitates the rapid development of Node
based Web applications.
Core features of Express framework
● Allows to set up middlewares to respond to HTTP
Requests.
● Defines a routing table which is used to perform
different actions based on HTTP Method and
URL.
● Allows to dynamically render HTML Pages based
on passing arguments to templates.
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
● This command saves the installation locally in
the node_modules directory and creates a
directory express inside node_modules.
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.
● multer − This is a node.js middleware for handling
multipart/form-data
Hello world Example
● Following is a very basic Express app which
starts a server and listens on port 8081 for
connection.
● This app responds with Hello World! for
requests to the homepage. For every other
path, it will respond with a 404 Not Found.
Request and Response
● Express application uses a callback function whose
parameters are request and response objects.
● The request object represents the HTTP request
and has properties for the request query string,
parameters, body, HTTP headers, and so on.
● The response object represents the HTTP response
that an Express app sends when it gets an HTTP
request.
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).
Body Parser
Express/Connect top-level generic
● var express = require('express')
● var bodyParser = require('body-parser')
● var app = express()
● Parse application/x-www-form-urlencoded
○ app.use(bodyParser.urlencoded({ extended: false }))
● Parse application/json
○ app.use(bodyParser.json())