Building API
With Node JS and Express
Cakra Danu Sedayu / Full Stack Developer
@dilo_medan groups/DevCMedan
CAKRA DANU SEDAYU
Fullstack Developer
mashara.id
cakra.ds@gmail.com
@cakrads
Rahasia Disesuaikan untuk nama perusahaan Versi 1.0
Basic
Core Engine
Theory Non-Blocking IO
How To Use?
Traditional Website
Modern Website
RESTful API
HTTP Verbs
Design Endpoints
Express JS
Practice
Daftar Isi
Node JS API Show Time
Ingredients
download and install
download and install
npm -i express
npm -i mongoose
npm install -g nodemon
npm install body-parser
download and install
Basic Node JS
Simple Word :
Server-side Javascript
!(Simple World):
High-performance
Network Applications Framework
CORE ENGINE
● JS V8 Engine by Google
● Non-Blocking I/O
● Event Loop
Theory Non-
Blocking IO
Traditional I/O
- Synchronous
- Wait for each operation to complete, after that executes next
operation
- Step by Step executions
- Asynchronous
- Never waits each operation to complete, executes all at one
time.
- Callbacks used to handle result
Non-blocking I/O
What can we do with
NODE JS
● HTTP Server
● TCP Server
● DNS Server
● Static File Server
● Web Chat Application (GTalk)
● Desktop GUI Application
Who uses Node.JS (in production)
0
1
source : https://github.com/nodejs/node-v0.x-archive/wiki/Projects,-Applications,-and-Companies-Using-Node
Rahasia Disesuaikan untuk nama perusahaan Versi 1.0
Basic
Core Engine
Teory Non-Blocking IO
How To Use?
Review
Node JS
How
to USE ?
Concept
● Use Modules
● One or more modules libraries are
called PACKAES
● To Install PACKAGES, we use NPM
(Node Package Manager)
Let’s
Practice
- Install Node JS (engine and NPM)
- chec installation in terminal or command promt
- $ node -v
- $nom -v
Installation
Try install Moment
https://momentjs.com/
$ npm i node-modules
$ node server.js
Make HTTP Server
Result:
API
Traditional Website
image source and modified : https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/00/56/73/3225.NoAPIArchitecture.PNG
Modern Website
image source and modified : https://farm1.staticflickr.com/907/41762807282_03cbff0e4f_b.jpg
image source and modified : https://farm1.staticflickr.com/907/41762807282_03cbff0e4f_b.jpg
Modern Architecture Use API
RESTful API
● API - Application Programming Interface
● REST API - REpresentational State Transfer
● RESTful sudah Pasti REST
● RESTful use HTTP verbs
HTTP Verbs
HTTP VERB CRUD
POST CREATE
GET READ
PUT UPDATE
DELETE DELETE
RESTful API VS Normal API
table : users
CRUD URL Normal URL RESTful
Create users/create users
Read users/read/id users/id
Update users/update/id users/id
Delete users/delete/id users/id
Rahasia Disesuaikan untuk nama perusahaan Versi 1.0
Traditional Website
Modern Website
RESTful API
HTTP Verbs
Review
API
BUILD API
USING EXPRESS
Design Endpoints
Method Endpoint Description Data
GET api/users List of users [{}, {}, {}]
GET api/users/:id View a users {}
POST api/users Create a new users {“msg”: “”}
PUT api/users/:id Update a user {“msg”: “”}
DELETE api/users/:id Delete a user {“msg”: “”}
Express JS
Structure
api-node-express
models
users.js
controllers
users.js
routes.js
server.js
api-node-express
api
users
usersController.js
usersModel.js
Type 1 : Type 2 :
CREATE - METHOD POST
Default : Use body-parser :
read more about body-parser in :
https://www.npmjs.com/package/body-parser
READ - METHOD GET
UPDATE - METHOD PUT
DELETE - METHOD DELETE
● No SQL Database
● Mongoose (Library)
- Create Schema in Model
All data will select by email
email = id
- Create in Controller
- Read in Controller
All data will select by email
email = id
- Update in Controller
All data will select by email
email = id
- Delete in Controller
All data will select by email
email = id
Terima kasih.

Build RESTful API Using Express JS

Editor's Notes

  • #24 REST ( Representational State Transfer) itu arsitektur sebuah software, sedangkan RESTful API itu merupakan salah satu model implementasi dari web service. RESTful API merupakan implementasi dari API. RESTful itu protokol/aturan untuk melakukan REST. Jadi RESTful itu udah pasti REST, namun REST belum tentu bisa disebut RESTful.
  • #29 REST ( Representational State Transfer) itu arsitektur sebuah software, sedangkan RESTful API itu merupakan salah satu model implementasi dari web service. RESTful API merupakan implementasi dari API. RESTful itu protokol/aturan untuk melakukan REST. Jadi RESTful itu udah pasti REST, namun REST belum tentu bisa disebut RESTful.
  • #30 Create Folder api-node-express npm install express --save check in package.json Try make Server: console.log(`Server running at http://${hostname}:${port}/`); Ada Pertanyaan??
  • #31 Express memberikan kita flexiblelitas untuk membuat API/routing kita bisa bentuk MVC bisa 1 folder aja atau 1 file di server.js tetepi kita anggap proyek kita ini adalah proyek besar, jadi kita membutuhkan structure yang baik