Six Myths about Ontologies: The Basics of Formal Ontology
Node.js Express Framework
1. Objective
What is Express JS?
Express JS Installation
Bootstrap Express in an application with Demo
Configuration and standard settings
Object of Express JS
Routing in Express
Request and Response Object in Express
Render Views and Template Engine with Demo
Middleware in Express
Demo
2. What is Express JS?
Express.js is a Node js web application server
framework, which is specifically designed for
building single-page, multi-page, and hybrid web
applications.
Express is the backend part of something known
as the MEAN stack.
1) MongoDB
2) Express.js
3) Angular.js
4) Node.js
3. Continue
The Express.js framework makes it very easy
to develop an application which can be used to
handle multiple types of requests like the GET,
PUT, and POST and DELETE requests.
Express FrameWork help a developer to quickly
build web sites with a lower the learning
curve.
4. Installation
Install with NPM
npm install express
This command will create folder in
node_modules and install the latest stable
express module over there. if you want to specify
the version you can pass @versionnumnber.
To load express
var express = require(‘express’);
5. Usage
Express module is factory for applications
var app = express();
Applications have many methods for
configuring
-- are also functions 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 JS ready for port
3000');
});
7. Standard settings used by
Express(with defaults)
1. env – application mode(process.env.NODE_ENV || 'development'
app.engine('html', require('ejs').renderFile);
2. trust proxy – trust headers set by reverse proxies(false)
app.set('trust proxy', '192.168.102.84');
3.jsonp calllback name – query string param name (callback)
4. json replacer – callback for JSON stringification(null)
5. case sensitive routing - /foo and /FOO are different (true)
6.strict routing - /foo and /FOO are different (false)
7. view cache – cache compiled views (true when env is production)
8. view engine – default entension fo views(no default)
9. views – directory containing views (./views)
8. Object of Express
There are four main object of Express:
1. Application (app)
2 .Request (req)
3 .Response (res)
4 .Router ( express.Router)
9. Routing in Express
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).
SYNTAX:
app.METHOD(PATH,HANDLER);
METHOD = GET/POST/PUT/DELETE etc
10. Routing Example
Routes are defined with URL pattern and callback
Functions
app.get('/login', function(req, res){
// render form
});
app.post('/login', function(req, res){
// verify credentials and issue cookie
});
11. Route Pattern
Patterns can be strings or regular expressions
Strings can contain param placeholders
app.get('/api/user/:id', function(req, res){
var userId = req.params.id;
//load user info and return JSON
});
12. Requests
Request objects expose details of HTTP requests:
req.params – route parameters in URL
req.query – query string parameters
req.get() - get header value
req.cookies() - cookie object( requires middleware)
req.body() - parsed body (requires middleware)
req.is() - text content type of request body
req.accepts() - content nagotiation for response
req.url – URL that matched current route
req.originalUrl – original url as sent by client
req.protocol – http or https
req.secure – true if protocol is https
req.host – value in host header
req.subdomains – subdomains of host
req.path – path in URL
req.xhr – true if requested with XMLHttpRequest
13. Responses
Many ways to respond to requests
res.set() – set response headers
res.cookie() and res.clearCookie() – modify response cookies
res.redirect() - issue 301 or 302 redirect to URL
red.send() - write status with string/Array/Object/Buffer
res.json() - stringify JavaScript value
res.jsonp() - send JavaScript value to callback function
res.sendfile() - stream contents of file
res.download() – stream file with content-disposition: attachment
res.render() – render view with pluggable view engine
15. Middleware in Express
MiddleWare performs the process before sending the
request to the router. For Example, Authenticate before
executing admin panel pages.
You can transfer the control to the Next MiddleWare using
the next() function.
Types of MiddleWare:
1. Application level MiddleWare
2. Router level Middleware
3. Error handling Middleware
4. Built In MiddleWare
5. Third Party MiddleWare
16. Third Party MiddleWare:
In Express JS 4, Middleware is not part of core modules.
So you have to install all middleware when require
List of Third Party MiddleWare.
1. Express-session :
2. request.Object:
3. Body-parser:
SYNTAX:
npm install body-parser
18. Application Middleware
You can have multiple application level MiddleWare.
Application level middleware is refers app on MiddleWare.
Express framework provides app.METHOD. Here is the list
of Methods:
1. app.get
2. app.post
3. app.put
19. Router Level MiddleWare
The Route MiddleWare is work as a separate component
in Express4.
Here in Route MiddleWare, express binds instance of
express.router object. This MiddleWare is mainly used for
configuring better routing.
20. Router Example
var express = require( 'express' );
var router = express.Router ();
router.use( function(req, res, next){
console.log( 'This is test routing');
next();
});
router.get( '/', function( req, res ){
res.send( 'This is GET Route' );
});
module . exports = router ;