Rest api with node js and express

16
RESTful API with Node JS and Express

Transcript of Rest api with node js and express

Page 1: Rest api with node js and express

RESTful API with Node JS and Express

Page 2: Rest api with node js and express

Agenda▷ What is API?▷ Contrasting common types of APIs▷ What is Node JS and Express?▷ Installing Express▷ Coding Session▷ Q & A ▷ Workshop

Page 3: Rest api with node js and express

1.What is API?

Page 4: Rest api with node js and express

“An API is a precise specification of the

programming instruction and standards to access a

web based software or web tool acting as a link between

the programmer and the application.

Page 5: Rest api with node js and express

What is an API?▷ Acronym for Application Program

Interface.▷ Acts as a bridge between the

programmer and application.▷ Takes specific requests predefined

when created.▷ Verifies the requests and then

processes the data.▷ Gives detailed info on what and

how the request are to be made.

Page 6: Rest api with node js and express

REST APIs

Page 7: Rest api with node js and express

▷ REST stands for Representational State Transfer. (ReST)▷ It is not a framework but Architectural Principle.▷ Uses HTTP requests which is oriented around verbs and

resources ( GET, POST, PUT, DELETE)▷ The verbs are applied to resources ( data )

POST something to database.GET something from Users.DELETE something from clients.

▷ The request consist of Headers, body and methods.▷ The response consists of Status Code, Headers and the

body.▷ Caching and Stateless.▷ Data represented mostly through HTML / XML / JSON.

REST APIs

Page 8: Rest api with node js and express

Contrasting types of APIs

REST▷ Stands for

Representational State Transfer.

▷ Is an Architectural principle.

▷ Permits different data formats.

▷ Better Performance and Scalability.

▷ Supports Caching .▷ Limited to single HTTP

transaction. Expects user to retry if something fails.

▷ Used in all type of system apart from some where high security risk are present.

SOAP▷ Stands for Simple Object

Access Protocol.▷ Is a protocol.▷ Permits only XML.▷ Scalable but at a very

minimum level.▷ Doesn’t support caching.▷ Can ensure ACID

transactions.▷ Used mostly in High risk

enterprise softwares and banking softwares.

Page 9: Rest api with node js and express

About Node.js

Node.js is not a silver-bullet new platform that will dominate the web development world.

Instead, it’s a platform that fills a particular need

Page 10: Rest api with node js and express

What is challenge in web development?Thread handling.

Page 11: Rest api with node js and express

Why Node.JS ?

Open Source

Non -Blocking

Large communit

y

Page 12: Rest api with node js and express

Asynchronous I/O

Page 13: Rest api with node js and express

Blocking & Non-Blocking CodeBlocking Code Examplevar fs = require("fs");

var data = fs.readFileSync('input.txt');

console.log(data.toString());console.log("Program Ended");

Output

Everything in the file input.txt

Program Ended

Non-Blocking Code Example

var fs = require("fs");

fs.readFile('input.txt', function (err, data) { if (err) return console.error(err); console.log(data.toString());});

console.log("Program Ended");

Output

Program Ended

Everything in the file input.txt

Page 14: Rest api with node js and express

Basic Example

var http = require("http");

http.createServer(function(request, response){response.writeHead(200, {'Content-Type':

'text/html'});response.end('<h1>Hello World</h1>');

}).listen(1234);

console.log("Server running at port 1234");

Page 15: Rest api with node js and express
Page 16: Rest api with node js and express

Thank You