building apis with node.js and monogdb
TRANSCRIPT
APIs are CoolCreate them with Node.js and MongoDB
Charlie Key@[email protected]
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
Build APIs to Create Applications Faster
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
Documenting Your APISmall Sidebar
It can be great to “blueprint” your API before building.
Let’s Get to Work
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.
MobileApp
Backend
Express mongoose
RESTAPICalls
Architecture Overview
Company ModelProperties of a Company
• Name
• Description
• Location
• Founded Date
• Stock Ticker
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
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
Demo
Consuming from ClientLots of options for consuming
• Web Frontend
• Native Mobile
• Desktop App
• NativeScript
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);
});
Demo App
Additional ThinkersAuthentication of API
• Passport npm Module
Testing
• Mocha npm Module
• Chai npm Module
Questions?
Charlie Key@[email protected]