Web APIs - Intro 9 ● Transfer data only (mostly JSON) ● Merge data into HTML on client ● Less response time ● Web server can handle more incoming requests
Web APIs - The Modern Way - REST API, HTTP API, whatever API - Model functional services with dedicated interface - Can use several other services for data aggregation - Secured by tokens - Consumable by every client which speaks HTTP(S) 11
Web APIs - Advantages ● Client can be deployed offline ○ Like Apps we use every single day ○ Single-Page-Applications (Angular 2, React) ● Only data is needed after initial loading ● Flexible hosting environments ○ Azure ○ Heroku ○ Digital Ocean 12
Web APIs - Basics ● HTTP verbs ○ GET: pull data from the web server ○ PUT: update data on the web server ○ POST: create data on the web server ○ DELETE: delete data on the web server ● HTTP web server ● Uniform Resource Locator (URL) That’s all we need. 13
Web APIs - Interface Example: https://localhost/api/blog/11 Our identifier for the data. GET gets the blog article with id 11 PUT updates the blog article with id 11 DELETE deletes the blog article with id 11 POST creates a blog article (in this case the web server creates the id) 14
More examples: ● GET /api/blog - gets all blog articles ● DELETE /api/user - deletes all users Possibility to define filters, orders, paging with query parameters: ● GET /api/blog?search=cat - get all blog articles with its content “cat” ● GET /api/blog?orderBy=date&direction=desc - get all blog articles ordered by date descending ● GET /api/blog?page=3 - gets the third page Web APIs - Interface 15
Web APIs - Restify ● Small Framework (similar to express) for building Web APIs ○ http://restify.com ● npm module: const restify = require('restify'); 18
Web APIs - Restify Server const server = restify.createServer( name: ‘myServer’ ); // For parsing the body automatically to a nice JSON object server.use(restify.bodyParser()); server.listen(1337, () => { console.log(`${server.name} is listening at ${server.url}`); }); 19