Introduction to RESTful Web APIs
- Representational state transfer:
- Originally for accessing and manipulating textual representations of Web resources using a set of stateless operations
- Today: More generic, encompassing every entity that can be identified, named, addressed or handled, in any way whatsoever, on the Web
- Architectural pattern, not a standard
- Request-response pattern
- Today, HTTP-based RESTful APIs dominate
- URLs for addressing
- JSON, sometimes XML for representing data elements
- Standard HTTP methods aka verbs (e.g. GET, PUT, POST, and DELETE)
- Standard HTTP status codes for representing results
- HTTP header fields (standard or custom) for sending parameters
- TLS for encrypting data in-transit
- API Clients
- Postman
- Insomnia
- REST Client in Visual Studio Code
- Web Debugger
- HTTP Request and Response Service
<!--#include file="rest-fundamentals/0010-rest-clients/pokeapi.http" -->
<!--#include file="rest-fundamentals/0010-rest-clients/northwind.http" -->
Exercise: Try this sample with different REST clients
<!--#include file="rest-fundamentals/0010-rest-clients/post.http" -->
Exercise: Try this sample with different REST clients
- Stateless
- No client context stored on the server
- Each request is complete
- Cacheable
- Responses explicitly indicate their cacheability
- Layered System
- Client cannot tell if connected directly to the server (e.g. reverse proxies)
- URIs
- Resources are identified using Uniform Resource Identifiers (URIs)
- Resource representation
- XML, JSON, Atom - today mostly JSON
- Old but still relevant: XMLHttpRequest
- Newer, but only in modern browsers: fetch
<!--#include file="rest-fundamentals/0020-rest-client/app-promise.js" -->
With async/await
:
<!--#include file="rest-fundamentals/0020-rest-client/app.js" -->
With jQuery:
<!--#include file="rest-fundamentals/0020-rest-client/app-jquery.js" -->
- In practice, frameworks are used for that
- Here: Express.js
- Larger framework, not just for RESTful Web APIs
- Very commonly used
- Lots of plugins
- Installation
npm install express
- For TypeScript:
npm install @types/express --save-dev
RESTful Web API with Express.js
<!--#include file="rest-fundamentals/0030-express-basics/app.ts" -->
<!--#include file="rest-fundamentals/0030-express-basics/request.http" -->
RESTful Web API with Express.js
express()
function- Creates an Express application
- Documentation
- Application
- Represents the Express application
- Created with
express()
- Documentation
request
object- Represents the HTTP request
- Use it to get headers, parameters, body, etc.
- Documentation
response
object- Represents the HTTP response
- Use it to build response (e.g. status, headers, body, etc.)
- Documentation
Express.js Examples
<!--#include file="rest-fundamentals/0040-express-verbs/app.ts" -->
Express.js Examples (cont.)
<!--#include file="rest-fundamentals/0040-express-verbs/data.ts" -->
<!--#include file="rest-fundamentals/0040-express-verbs/get-all.ts" -->
Express.js Examples (cont.)
<!--#include file="rest-fundamentals/0040-express-verbs/get-single.ts" -->
Express.js Examples (cont.)
<!--#include file="rest-fundamentals/0040-express-verbs/post.ts" -->
Express.js Examples (cont.)
<!--#include file="rest-fundamentals/0040-express-verbs/delete-single.ts" -->
- Lightweight in-memory key-value store
- Fast and easy to use
- Works in...
- ...browser
- ...apps
- ...Node.js on the server or in the command line
- Persistence adapter can write data to disk/indexeddb
<!--#include file="rest-fundamentals/0050-express-loki/db.ts" -->
Express.js Examples with Lokijs
<!--#include file="rest-fundamentals/0050-express-loki/app.ts" -->
Express.js Examples with Lokijs (cont.)
<!--#include file="rest-fundamentals/0050-express-loki/get-all.ts" -->
Express.js Examples with Lokijs (cont.)
<!--#include file="rest-fundamentals/0050-express-loki/get-single.ts" -->
Express.js Examples with Lokijs (cont.)
<!--#include file="rest-fundamentals/0050-express-loki/post.ts" -->
Express.js Examples with Lokijs (cont.)
<!--#include file="rest-fundamentals/0050-express-loki/delete-single.ts" -->
Express.js Server-Side Rendering
- Express.js also supports server-side rendering
- HTML is generated on the server-side
- Many different template engines are supported
- Here we use handlebars
- Server-Side Rendering can be combined with client-side logic
- Client is a browser
- Most of the business logic runs on the server
- Minor parts of the logic runs on the client
(e.g. form validation in JavaScript) - Server accesses databases and external services
- Minor parts of the logic runs on the client
- Server generates HTML, CSS, JavaScript
- Client can be a browser
- Anything that can speak HTTP, JSON, etc.
(e.g. mobile app, CLI, server, desktop app, IoT device)
- Anything that can speak HTTP, JSON, etc.
- Static HTML/CSS/JS for SPA
- Logic
- HTTP Web API requests for running server-side business logic
- View logic (e.g. manipulating DOM) runs on client
- JSON for transmitting data
Express.js Server-Side Rendering
<!--#include file="rest-fundamentals/0060-express-server-side-html/src/app.ts" -->
Express.js Server-Side Rendering (cont.)
<!--#include file="rest-fundamentals/0060-express-server-side-html/src/views/index.hbs" -->
- Want to know more? Read/watch...
- Exercises