Presentation is loading. Please wait.

Presentation is loading. Please wait.

Node.js Express Web Applications

Similar presentations


Presentation on theme: "Node.js Express Web Applications"— Presentation transcript:

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

2 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

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

4 Getting Started with Express
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

5 Getting Started with Express

6 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

7 Creating a Hello World Express Application

8 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

9 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' setHeaders – allows a function to be set for specifying custom headers on static file requests

10 Serving Up Static Files

11 Routing Requests 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

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

13 Routing Requests

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

15 Working with URL Parameters and Query String Values

16 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

17 Working with a Request Body
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

18 Extracting URL Encoded and JSON Data from the Request Body

19 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 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

20 Enabling Cookies with Middleware

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

22 Authentication with Passport
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

23 Authentication with Passport

24 Passport Authentication with Session Cookies

25 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


Download ppt "Node.js Express Web Applications"

Similar presentations


Ads by Google