17
APIs are Cool Create them with Node.js and MongoDB Charlie Key @zwigby [email protected]

Building APIs with Node.js and MonogDB

Embed Size (px)

Citation preview

Page 1: Building APIs with Node.js and MonogDB

APIs are CoolCreate them with Node.js and MongoDB

Charlie Key@[email protected]

Page 2: Building APIs with Node.js and MonogDB

What is an API?Application Programming Interface

API is a set of routines, protocols, and tools for building software applications. The API specifies how software components should interact and APIs are used when programming graphical user interface (GUI) components.

A good API makes it easier to develop a program by providing all the building blocks. A programmer then puts the blocks together.

http://www.webopedia.com/TERM/A/API.html

Page 3: Building APIs with Node.js and MonogDB

Build APIs to Create Applications Faster

Page 4: Building APIs with Node.js and MonogDB

Designing APIs1. Plan It Out

2. Speak HTTP (GET, POST, PUT, PATCH, DELETE)

3. Write Documentation

4. Think About Versioning

5. Understand Security

6. Use HTTP Status Codes

7. Return Consistent Errors

8. Utilize Paginating Results

Page 5: Building APIs with Node.js and MonogDB

Documenting Your APISmall Sidebar

It can be great to “blueprint” your API before building.

Page 6: Building APIs with Node.js and MonogDB

Let’s Get to Work

Page 7: Building APIs with Node.js and MonogDB

The API of the DayLet’s build an API to keep track of our favorite companies.

Maybe a bit contrived but will include CRUD methods so gets to the point.

Page 8: Building APIs with Node.js and MonogDB

MobileApp

Backend

Express mongoose

RESTAPICalls

Architecture Overview

Page 9: Building APIs with Node.js and MonogDB

Company ModelProperties of a Company

• Name

• Description

• Location

• Founded Date

• Stock Ticker

Page 10: Building APIs with Node.js and MonogDB

Setup Project> mkdir my-new-project && cd my-new-project> npm init> npm install express --SE> npm install body-parser --SE> npm install mongoose --SE> touch index.js> mkdir routes> mkdir models

Page 11: Building APIs with Node.js and MonogDB

Create MongoDB Database1. Create a New Database

2. Name that Database

3. Decide on Region

4. Enter Default Username

5. Enter Default Password

6. Click Button

Page 12: Building APIs with Node.js and MonogDB

Demo

Page 13: Building APIs with Node.js and MonogDB

Consuming from ClientLots of options for consuming

• Web Frontend

• Native Mobile

• Desktop App

• NativeScript

Page 14: Building APIs with Node.js and MonogDB

NativeScriptEasy to consume REST API

http.getJSON("http://localhost:3000/company").then(function (r) { // Add companies to listfor(var i = 0; i < r.length; i++) {

companies.push(r[i]);}

}, function (e) { // Argument (e) is Error!console.log(e);done(e);

});

Page 15: Building APIs with Node.js and MonogDB

Demo App

Page 16: Building APIs with Node.js and MonogDB

Additional ThinkersAuthentication of API

• Passport npm Module

Testing

• Mocha npm Module

• Chai npm Module

Page 17: Building APIs with Node.js and MonogDB

Questions?

Charlie Key@[email protected]