Javascript Basic RESTful

16
Jakarta JS Back to Basic of Node.js and Reactivity RESTful API with Node.js + MongoDB

Transcript of Javascript Basic RESTful

Page 1: Javascript Basic RESTful

Jakarta JS

Back to Basic of Node.js and Reactivity

RESTful API with Node.js + MongoDB

Page 2: Javascript Basic RESTful

About Me

• Name : Sofian Hadiwijaya

• Email : [email protected]

• Blog : http://www.sofianhw.com

• Id : sofianhw

Page 3: Javascript Basic RESTful

Basic - RESTfulWikipedia, that infallible source, defines Representational State Transfer (REST) as: an architectural style that abstracts the architectural elements within a distributed hypermedia system.

Page 4: Javascript Basic RESTful

Basic - RESTfulFour basic design principles from IBM's developerWorks website

1.Use HTTP methods explicitly2.Be Stateless3.Expose directory structure-like URIs4.Transfer XML or JSON, or Both

Page 5: Javascript Basic RESTful

Basic - Express JSA minimal and flexible node.js web application framework, providing a

robust set of features for building full web applications and/or APIs.

$ npm install express• Middleware• Routing

Page 6: Javascript Basic RESTful

Basic - ExpressExpress Hello World

Page 7: Javascript Basic RESTful

Basic - ExpressA router object is an isolated instance of middleware and routes, capable only of performing middleware and routing functions. A router behaves like middleware itself, so you can use it as an argument to app.use() or as the argument to another router’s use() method. Every Express application has a built-in app router:

Page 8: Javascript Basic RESTful

Basic - ExpressChainable route handlers for a route path can be created using app.route().Since the path is specified at a single location, it helps to create modular routes and reduce redundancy and typos.

Page 9: Javascript Basic RESTful

Basic - Mongooseelegant mongodb object modeling for node.js

$ npm install mongoose

Page 10: Javascript Basic RESTful

Build Basic RESTfulVery simple file structure

Defining Node Packages

Page 11: Javascript Basic RESTful

Build Basic RESTfulClone repository

$ git clone https://github.com/sofianhw/simple-restful-nodejs.git

Install packages with $ npm install

Running Script $ npm start

Page 12: Javascript Basic RESTful

Model

Page 13: Javascript Basic RESTful

Routes

Page 14: Javascript Basic RESTful

Basic Testing - MochaMocha is a simple, flexible, fun JavaScript test framework for node.js and

the browser.

$ npm install -g mocha

Page 15: Javascript Basic RESTful

Basic Testing - Shouldshould is an expressive, readable, framework-agnostic assertion library. The main goals of this library are to be expressive and to be helpful. It keeps your test code clean, and your error messages helpful.

$ npm install should

Page 16: Javascript Basic RESTful

Basic Testing - SupertestHTTP assertions made easy via super-agent

$ npm install supertest