Header Mastering Node js Part 6 Node js

  • Slides: 25
Download presentation
Header Mastering Node. js, Part 6 Node. js Express Web Applications Microsoft Virtual Academy

Header Mastering Node. js, Part 6 Node. js Express Web Applications Microsoft Virtual Academy Eric W. Greene Produced by

Course Overview Getting Started with Express Working with Static Files Configuring Routes Extracting Data

Course Overview Getting Started with Express Working with Static Files Configuring Routes Extracting Data from the URL Working with a Request Body Working with Cookies Authentication with Passport

Getting Started with Express The course assumes some basic knowledge of Node. js and

Getting Started with Express The course assumes some basic knowledge of Node. js and the Java. Script programming language To run the examples on your machine, you will need Node. js installed Node. js can be downloaded from here: https: //nodejs. org Install version 6 or later If you do not understand the basics of these technologies, then watch the Wintellect. NOW courses, Introduction to Node. js, Node. js Modules & Node. js Packages

Getting Started with Express is a Web Framework used to configure Node's HTTP module

Getting Started with Express is a Web Framework used to configure Node's HTTP module Fast Un-opinionated Minimalist Express is distributed via an NPM package Express should be installed locally to each project, not globally To install: npm install express

Getting Started with Express https: //expressjs. com

Getting Started with Express https: //expressjs. com

Getting Started with Express Provides support for routing and serving static files Almost all

Getting Started with Express Provides support for routing and serving static files Almost all other functionality is provided through third-party middleware modules Processing request body – Body Parser Working with cookies – Cookie Parser Using session – Express Sessions Handling Authentication - Passport

Creating a Hello World Express Application

Creating a Hello World Express Application

Working with Static Files Serving up static files is provide directly by Express itself

Working with Static Files Serving up static files is provide directly by Express itself To serve up static files, the express. static function is invoked with the folder path to the files to be served

Working with Static Files Static file handling supports several options which can be passed

Working with Static Files Static file handling supports several options which can be passed as a second argument to the static function call, here are two of those options index – specifies the file name of the default file to serve from a folder if no file is specified, default value is 'index. html' set. Headers – allows a function to be set for specifying custom headers on static file requests

Serving Up Static Files

Serving Up Static Files

Routing Requests can be routed based upon the following factors HTTP Method – GET,

Routing Requests can be routed based upon the following factors HTTP Method – GET, POST, PUT, DELETE and many more URL Path – Path of the request URL can be matched against patterns Routes are match based upon the order they are registered Each match is handled by a function which is passed a request and a response object allowing the handler to access information from the request and contribute to the generation of the response Each handler can pass the request along via the next function

Routing Requests Matches all HTTP verbs Matches the GET verbs Functions for POST, PUT,

Routing Requests Matches all HTTP verbs Matches the GET verbs Functions for POST, PUT, DELETE as well as app. METHOD are also available

Routing Requests

Routing Requests

Extracting Data from the URL Two kinds of data are available in the URL

Extracting Data from the URL Two kinds of data are available in the URL Parameters – exposed as params property of the request object Query String Parameters – exposed as the query property on the request object URL Parts | | | http: // www. some-domain. com : 8080 /person/2 ? detailed=true #home Protocol Domain Name / IP Address Port Path Query String Client-Side

Working with URL Parameters and Query String Values

Working with URL Parameters and Query String Values

Working with a Request Body Express does not support processing of the request body

Working with a Request Body Express does not support processing of the request body Instead, middleware can be used to process the request body Body Parser – used to process URL encoded and JSON content Multer (and many others) – used to process file uploads The request body is a Node. js stream, and the stream data can be read and processed manually

Working with a Request Body Parser determines whether to process the request body as

Working with a Request Body Parser determines whether to process the request body as URL encoded of JSON data based upon the request Content-Type Body Parser makes the data available through the body property on the request object Body Parser should be loaded before any routes which need access to the request body data

Extracting URL Encoded and JSON Data from the Request Body

Extracting URL Encoded and JSON Data from the Request Body

Working with Cookies Express provides the ability to write cookies to the response, but

Working with Cookies Express provides the ability to write cookies to the response, but middleware is needed to parse cookies in the request The most common middleware is named Cookie Parser https: //github. com/expressjs/cookie-parser It is installed using: npm install cookie-parser The cookie parser middleware must be loaded before any routes which need access to cookies The response cookie function writes a cookie to the response by using the Set-Cookie header

Enabling Cookies with Middleware

Enabling Cookies with Middleware

Authentication with Passport Authentication for Express is commonly provided by Passport provides many different

Authentication with Passport Authentication for Express is commonly provided by Passport provides many different strategies for authenticating users User accounts stored in local databases Facebook, Twitter, Google OAuth and OAuth 2 Providers WS Federation and SAML 2

Authentication with Passport relies express-session middleware to use session-based cookies to track logged in

Authentication with Passport relies express-session middleware to use session-based cookies to track logged in users Sessions are not required if credentials like an API-key are provided on every request Passport is well suited for user applications as well as REST services

Authentication with Passport http: //passportjs. org

Authentication with Passport http: //passportjs. org

Passport Authentication with Session Cookies

Passport Authentication with Session Cookies

Conclusion Express is a Node. js web framework It makes configuring the Http Server

Conclusion Express is a Node. js web framework It makes configuring the Http Server a lot easier Provides routing and static file delivery out of the box Relies upon third-party middleware to process the request body, cookies, sessions, authentication, etc… Popular, easy to use, and very flexible