Presentation is loading. Please wait.

Presentation is loading. Please wait.

Node.js Express Web Services

Similar presentations


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

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

2 Course Overview Getting Started with Express REST Services
Configuring Routers Extracting Data from the URL Working with a Request Body JSON Web Token Authentication with Passport

3 Getting Started with Express REST Services
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 REST Services
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 REST Services

6 Getting Started with Express REST Services
Provides support for routing and serving static files Almost all other functionality is provided through third-party middleware modules Processing request body – Body Parser Handling Authentication – Passport JSON Web Tokens – Passport JWT

7 Getting Started with Express REST Services
REST – Representational state transfer The concept of REST originated from Roy Fielding's doctoral dissertation from his studies at UC Irvine (source: REST is a convention not a standard such as SOAP Organizations implement REST web services differently, but there are common goals and patterns

8 What are the Key Concepts of REST Services?
Client-Server – Client and server are ignorant of each other's implementation and execution state Stateless – No state is maintained between REST requests, each request is completely independent from another request Cacheable – Responses to requests can be marked or not marked as cacheable, where possible caching should be used to improve performance

9 What are the Key Concepts of REST Services?
Interface - When implemented with HTTP, interaction is between a web client and web server, HTTP verbs indicate the action to perform, and URIs indicate which resource(s) that action is performed on

10 Conventions of REST Services
Two URIs: Collection URI and the Element URI Collection URI resource name> Element URI resource name>/<resource id> These two URIs combined with HTTP verbs provide the interface to all of the REST service operations

11 Conventions of REST Services
Most Common Operations GET <collection uri> - returns all resources GET <element uri> - returns one resource POST <collection uri> - inserts a new resource PUT <element uri> - replaces or inserts a new resource DELETE <element uri> - delete an existing resource

12 Conventions of REST Services
Update or Replace? Many times updating a resource is preferred over replacing; therefore, the PATCH verb with the element URI is commonly used for updating When updating, only the fields to be replaced are included request body For POST and PUT operations, the request body contains the data to be inserted or replaced Either XML or JSON can be used, but JSON is by far the most common

13 Using Express to implement REST Services
Using Express and its associated middleware, REST services can be configured and coded Supports: Common REST service HTTP verbs Extracting of element URI ids from the URL Handling of XML and JSON request body data (JSON is preferred and covered in this course) Authentication with JSON Web Tokens

14 REST Clients REST clients are tools designed to allow for easy interaction with REST services Unlike a standard web browser, the user can easily choose the HTTP verb, specify headers, and provide request bodies in almost any reasonable format

15 REST Clients REST clients will execute the request and typically deliver results in a raw or formatted output Popular REST Clients include… Postman - Advanced REST Client -

16 Creating a Hello World Express REST Service

17 Routing REST Service Requests
Express provides a powerful Router object which can be used to configure complex routes which detect HTTP verbs and URL path patterns Routers are really middleware, which are mountable on any path Route handlers have access to the request and response objects Routers provide a route method to define a URL path for the route and allows for easy configuring of handlers for common HTTP verbs

18 Configuring a Router

19 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 (not used as part of REST service conventions) URL Parts :8080 /person/2 ?detailed=true #home | Protocol Domain Name / IP Address Port Path Query String Client-Side

20 Working with URL Parameters

21 Working with a Request Body
Express does not directly 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 Without middleware, the request body is accessible through a Node.js stream, and the stream data can be read and processed manually

22 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 Most REST services today use JSON as the preferred data format

23 Extracting JSON Data from the Request Body

24 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 JSON Web Tokens (we will use this for REST service authentication)

25 Authentication with Passport

26 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 We will explore a REST service authentication without sessions using JSON Web Tokens

27 Authentication with Passport
For REST services, the JSON Web Token (JWT) strategy can be used To install this strategy: npm install passport-jwt JSON Web Tokens are on open standard for representing claims between two parties (RFC The JSON part of JWT means that JSON is the data format to store the claim Web Tokens part of JWT are the claim itself structure with a header, payload and signature

28 Authentication with Passport
JSON Web Tokens JSON Structure Header – typically contains the type of token and hash algorithm being used Payload – contains the claims themselves Signature – is a hash of the header and payload values, and is use to verify the sender More Information: Debugger Tool: (Click Debugger at the Top)

29 Passport Authentication with JSON Web Tokens

30 Conclusion Express is a Node.js web framework
It makes configuring the Http Server a lot easier Its routing and middleware capabilities greatly simplify the creation of REST services Through middleware, multiple authentication strategies are supported such as JSON Web Tokens Popular, easy to use, and very flexible


Download ppt "Node.js Express Web Services"

Similar presentations


Ads by Google