340 likes | 409 Views
This Edureka "Node.js Express tutorial" will help you to learn the Node.js express fundamentals with examples. Express.js is flexible and minimal node.js web application framework that provides robust set of features to develop mobile and web applications. It facilitates the rapid development of node.js applications. Below are the topics covered in this tutorial: <br><br>1) Why Express.js? <br>2) What is Express.js? <br>3) Express Installation <br>4) Express Routes <br>5) Express Middlewares
E N D
Agenda 1 2 Why Express? What is Express.js? 3 5 Demo Express Routes 4 Express Middlewares EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Node.js EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
What is Node.js ? ▪ Node.js is an open source runtime environment for server-side and networking applications and is single threaded. ▪ Uses Google JavaScript V8 Engine to execute code. ▪ It is cross platform environment and can run on OS X, Microsoft Windows, Linux and FreeBSD. ▪ Provides an event driven architecture and non blocking I/O that is optimized and scalable. EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Creating Server using HTTP Module EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
HTTP To use the HTTP server and client one must require('http') ▪ The HTTP interfaces in Node.js are designed to support many features of the protocol ▪ Import Required Modules Creating Server Parse the fetched URL to get pathname Request file to be read from file system Creating Header with content type as text or HTML Generating Response Listening to port: 3000 www.edureka.co/mastering-node-js EDUREKA ANGULARJS CERTIFICATION TRAINING
What is Express? EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
What is Express.js? Web application framework for Node.js Light-weight and minimalist Provides boilerplate structure & organization for your web-apps ▪ ▪ ▪ EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Express Installation EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Routes EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Routes Routing refers to the definition of application end points (URIs) and how they respond to client requests A route method is derived from one of the HTTP methods, and is attached to an instance of the express class Importing Express Module Creating Express Instance Callback function Path (route) Route Methods HTTP request HTTP response EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Routes app.all(), special routing method (not derived from any HTTP method) app.all() is used for loading middleware functions at a path for all request methods EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Route Path Route paths, in combination with a request method, define the endpoints at which requests can be made • Route paths can be strings, string patterns, or regular expressions. • The characters ?, +, *, and () are subsets of their regular expression counterparts. • EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Route Handlers Provide multiple callback functions which behave like middleware to handle a request Exception -> Callbacks might invoke next('route') to bypass the remaining route callbacks A single callback function can handle a route Next to bypass the remaining route callbacks Used to impose pre-conditions on a route, then pass control to subsequent routes (if no reason to proceed with the current route) EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Route Handlers Route handlers can be in the form of a function, an array of functions, or combinations of both Creating Functions Router handlers in form of functions & array of functions EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Response Methods Method Description res.download() Prompt a file to be downloaded. res.end() End the response process. Methods on the response object (res) for sending a response to the client • res.json() Send a JSON response. res.jsonp() Send a JSON response with JSONP support. Terminate the request-response cycle • res.redirect() Redirect a request. If none of these methods are called, the client request will be left pending • res.render() Render a view template. res.send() Send a response of various types. res.sendFile() Send a file as an octet stream. Set the response status code and send its string representation as the response body. res.sendStatus() EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Routes (app.route) Create chainable route handlers for a route path by using app.route() • Path is specified at a single location • Creating modular routes is helpful, as it reduces redundancy and typos • Creating chainable route GET Method POST Method PUT Method EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Routes (express.Router) Creates a router as a module Loads a middleware function ➢ Router class to create modular, mountable route handlers ➢ A Router instance is a complete middleware and routing system Defines Home Route Define About Route EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Middleware EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
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. next function is invoked to executes the middleware succeeding the current middleware Function B Request A Response A Request B Response B Response Object Next function Request Object Request C Response C Middleware Clients Server EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Middleware Middleware functions performs 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 • • • • EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Middleware Path (route) HTTP method HTTP request argument HTTP response argument Callback argument to the middleware function EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Middleware Uses the requestTime middleware function EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Application-level middleware Bind application-level middleware to an instance (app.use() & app.METHOD()) EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Router-level middleware Router-level middleware binds to an instance of express.Router() Works in the same way as application-level middleware • • Router middleware i.e. executed for every router request Router middleware i.e. executed for given path EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Error-handling middleware Error-handling middleware always takes four arguments: (err, req, res, next) next object will be interpreted as regular middleware and will fail to handle errors Define error-handling middleware functions in the same way as other middleware functions • • • Error Object Request Object Response Object Next Object EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Built-in middleware Except express.static, all of the middleware functions that were previously bundled with Express are now in separate modules express.static function is responsible for serving static assets such as HTML files, images, etc. • • serving static assets EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Independent Module These middleware and libraries are officially supported by the Connect/Express team: Modules Description body-parser Parse incoming request bodies in a middleware before your handler compression Node.js compression middleware connect-timeout Times out a request in the Express application framework cookie-parser Parse Cookie header and populate req.cookies with an object keyed by the cookie names cookie-session Simple cookie-based session middleware csurf Node.js CSRF protection middleware errorhandler Error handler middleware express-session Create a session middleware method-override Create a new middleware function to override the req.method property with a new value morgan Create a new morgan logger middleware function response-time Creates a middleware that records the response time for requests in HTTP servers serve-favicon Middleware for serving a favicon serve-index Serves pages that contain directory listings for a given path serve-static Middleware function to serve files from within a given root directory vhost Middleware function to hand off request to handle, for the incoming host EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Template Engines with Express EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Template Engines with Express Template engine enables you to use static template files in your application Easier to design an HTML page Popular template engines: Pug, Mustache, and EJS Express application generator uses Jade as its default • • • • Rending Template inside the application Declaring HTML template using Jade EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Template Engines with Express To render template files, set property in app.js : views, the directory where the template files are located • view engine, the template engine to use • EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Database Integration www.edureka.co/mastering-node-js EDUREKA ANGULARJS CERTIFICATION TRAINING
Database Integration Database systems supported by Express: Cassandra Couchbase CouchDB LevelDB MySQL MongoDB Neo4j PostgreSQL Redis SQL Server SQLite ElasticSearch • • • • • • • • • • • • EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Thank You … Questions/Queries/Feedback EDUREKA NODE.JS CERTIFICATION TRAINING www.edureka.co/angular-js