#Building an API with node and express
###Learning Objectives
- create a node app that returns an array of json data
- use url parameters in our app
- create an API endpoint that returns an individual entry
- create a middleware that checks the value of something
###What is an API?
APIs are the language of the modern web, our end goal is to be able to be able to create our own APIs using node and consume them with a front end framework.
Today we're going to create an API that lets a user access data about cars, we'll have a couple endpoints for this you should be able to access an array of cars by going to /cars
and be able to pull up an individual car by their id by going to /cars/1
or /cars/3
We do this by attaching json data to the response
object. This is the second parameter passed to our callback function in an express route function
###Doing this with node and express.
The basic idea is to respond to the request on a certain port, look through the url and do different things.
We're not going to be working with a database today so we're just going to hard code our data into an array of objects.
Lets get started by building a super simple express app for right now
Make a new workspace directory and cd into it,
Run npm init
in there and keep hitting enter, then we're going to run npm install express --save
Now we're going to touch a file server.js
and open that up, here is where we're going to write the basic code to make an express app
expresss = require('express');
var app = express();
var server = app.listen(8080, function() {
console.log('ready to rock!');
});
First lets create the object
Then we attach it to the response object
app.get('/dogs', function(request,response) {
var dog = { name: 'Fido', breed: 'Golden Retreiver' };
response.json(dog);
);
Why don't you build another route /student
that returns your name and your favorite Starbucks drink.
Not super heard right? Express and Node makes it super easy, we're working with Javascript so its natural to just return javascript objects.
Now I want to introduce the idea of url parameters
this is a way of parsing or looking through a url and saying that certain parts correspond to certain variables
For example
say you want to be able to select a certain dog by typing in their name, we want our site to respond to urls like
/dogs/fido
However, we also want it to respond to urls like
/dogs/max
Rather than hard coding all of these routes in, we say that whatever comes after dogs is going to be a variable.
We write our url as
/dogs/:name
Now any request to our server that follows this pattern will have have a property name
on req.params
,
Why don't we test it out and console log it to make sure
app.get('/dogs/:name', function(request,response) {
var dogName = request.params.name
console.log(dogName);
res.write('thanks for searching' + dogName);
});
Now assuming we have an array of dogs
var dogs = [
{ "name": "fido", "breed": "Labrador"},
{ "name": "rex", "breed": "Doberman"},
{ "name": "sparky", "breed": "pitbull"},
{ "name": "mocha", "breed": "Bichon Frise"},
{ "name": "tiara", "breed": "chihuahua" },
{ "name": "capper", "breed": "border collie"}
];
How could you search through it and only return that specific dog?
How could you return all the dogs?
Now this is a small example and later we'll get into how instead of returning hardcoded data we'll instead do a lookup from an API or from our own database
###Middleware I would like to introduce the concept of node middleware.
Middleware is basically a function that lives between the initial request, and the response.
Then we tell our express app to use our middleware we pass it three parameters, the request, the response, and next
app.use(function(request, response, next) [
// do some stuff and then
console.log(Date.now);
// call next to move forward and do everything else
next();
});
Does everyone understand the concept of node middleware?
How could we implement it so that you only allow users to see the different dogs if they provide a specific parameter? Why don't we only let users see dogs if the parameter dog_lover
is equal to true
(Hint, we can access url parameters like /site?foo=bar
by checking out request.query
)