Create Rest API in Nodejs

Post on 08-Jan-2017

1.247 views 0 download

Transcript of Create Rest API in Nodejs

CREATE REST API IN NODEJS

WITH EXPRESS FRAMEWORK AND MONGOOSE

ABOUT ME

Name : Irfan Maulana

Work in : PT Global Digital Niaga (Blibli.com)

Position : Software Development Engineer

+ JavaScript Enthusiast

+ Specialize in Web Front-End

+ New knowledge addicted

WHAT IS REST API• REST : Representational State Transfer• was defined by Roy Thomas Fielding in his 2000 PhD dissertation

"Architectural Styles and the Design of Network-based Software Architectures".

• It relies on a stateless, client-server, cacheable communications protocol -- and in virtually all cases, the HTTP protocol is used. (http://rest.elkstein.org/)

REST CONSTRAINTS• Client–server• Stateless• Cacheable• Uniform Interface

REST METHODS• GET• POST• PUT• DELETE

NODEJS• JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an

event-driven, non-blocking I/O model that makes it lightweight and efficient.

EXPRESS• is a minimal and flexible Node.js web application framework that provides a

robust set of features for web and mobile applications.

MONGOOSE• Elegant mongo DB object modeling for node.js

<LETS-START-CODE/>

SET UP DEPENDENCIES

BASIC REST IN EXPRESS• Require express framework

• Create app

• Our very first API

• And check it

CONNECT MONGODB WITH MONGOOSE• Require mongoose

• Create connection

• You can add handler state

CREATE MODEL IN MONGOOSE• Require connection

• Create schema

• Create model

USING MODEL AS ORM• Require model

• Save data using ORM

EXPRESS ROUTE (1)• Require express and get router instance

• Require model for ORM usage

• Create route for path / with GET method to get all data in database

EXPRESS ROUTE (2)• Create route for path / with POST method to insert new data

EXPRESS ROUTE (3)• Create route for path /:id with GET method to get data by ID

EXPRESS ROUTE (4)• Create route for path /:id with PUT method to update existing data with new data by ID

EXPRESS ROUTE (5)• Create route for path /:id with DELETE method to remove data by ID

• Create root path for all your API

TEST IN POSTMAN (1)

TEST IN POSTMAN (2)

TEST IN POSTMAN (3)

SOURCE CODE REPOSITORY

https://github.com/mazipan/nodejs-simple-restfull-with-express

THANK YOU

Linkedin : https://id.linkedin.com/in/irfanmaulanamazipan

Github : https://github.com/mazipan

Email : mazipanneh@gmail.com

Mobile : 0857-8123-8798