create a restful api with nodejs, express and mongodb

12
Tech Talk Friday, 13 February 2015

Upload: hengki-sihombing

Post on 16-Jul-2015

186 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Create a RESTful API with NodeJS, Express and MongoDB

Tech TalkFriday, 13 February 2015

Page 2: Create a RESTful API with NodeJS, Express and MongoDB

Create a RESTful API

Express +

Page 3: Create a RESTful API with NodeJS, Express and MongoDB

Roadmap• Express Overview • MongoDB Overview • Learn Express Route • ODM MongoDB with Mongoose • Handle CRUD for an item • Use the proper HTTP verbs to make it RESTful (GET,

POST, PUT, and DELETE) • Return JSON data

Page 4: Create a RESTful API with NodeJS, Express and MongoDB

Getting Started

$ curl https://raw.githubusercontent.com/creationix/nvm/v0.23.3/install.sh | bash

$ source ~/.nvm/nvm.sh

$ nvm install v0.10.36

$ nvm alias default 0.10.36

Install and Setup

Install Node.JS using NVM [Node Version Manager] https://github.com/creationix/nvm

Install MongoDB on OS X

Install MongoDB with Homebrew : $ brew install mongodb

http://www.mongodbspain.com/en/2014/11/06/install-mongodb-on-mac-os-x-yosemite/

Page 5: Create a RESTful API with NodeJS, Express and MongoDB

MongoDB MongoDB is a document database that provides high

performance, high availability, and easy scalability.

source : http://www.mongodb.org/about/introduction/

Schema Less Data strore in JSON-like documents with dynamic Providing flexibity during the development process

Built-in Javascript :)

Page 6: Create a RESTful API with NodeJS, Express and MongoDB
Page 7: Create a RESTful API with NodeJS, Express and MongoDB

“Representational State Transfer (REST) is a software architecture style consisting of guidelines and best practices

for creating scalable web services. REST is a coordinated set of constraints applied to the design of components in a

distributed hypermedia system that can lead to a more performant and maintainable architecture.”

http://en.wikipedia.org/wiki/Representational_state_transfer

What is REST

Page 8: Create a RESTful API with NodeJS, Express and MongoDB

Express Apps$ npm install

$ node server.js

Page 9: Create a RESTful API with NodeJS, Express and MongoDB

Express Route

curl -i -X POST -H 'Content-Type: application/json' -d '{}' http://localhost:3000/user

curl -i -X PUT -H 'Content-Type: application/json' -d '{}' http://localhost:3000/user

curl -i -X DELETE -H 'Content-Type: application/json' -d '{}' http://localhost:3000/user

curl -i -X GET http://localhost:3000/user

Page 10: Create a RESTful API with NodeJS, Express and MongoDB

MongoDB with MongooseJS$ npm install —save mongoose

Page 11: Create a RESTful API with NodeJS, Express and MongoDB

Create MongoDB Model

Page 12: Create a RESTful API with NodeJS, Express and MongoDB

$ git clone [email protected]:aredo/express-rest-api.git

Source Code