- Describe the concept of resources in relation to REST and APIs
- Identify RESTful conventions for API routes
- Explain what express is and why it's useful
- Build RESTful routes using express
-
What is a resource?
Your answer...
-
What are the five common actions of an API? What methods and status codes are typically used for the five common actions?
Your answer...
-
Build RESTful routes for one of the following resources: cards, dogs, engineers
Action Method Path Body _ _ _ _ _ Your answer...
-
Attept this REST Quiz!
-
How would you build RESTful routes for the following data?
const authors = [ { name: 'Haruki Murakami', books: [ { title: 'Hard-Boiled Wonderland and the End of the World' }, { title: 'The Wind-Up Bird Chronicle' } ] }, { name: 'Kurt Vonnegut' books: [ { title: 'Slaughterhouse-Five' } ] } ]
- Get all authors
- Get one author
- Add new author
- Get all books by author
- Get one book by author
- Add new book by author
- Edit one book by author
- Delete one book by author
- Challenge: Get a specific number of authors?
Your answer...
- Compare the two example HTTP web servers bellow:
const fs = require('fs')
const http = require('http')
const server = http.createServer(function(request, response) {
if(request.url === '/logo.png') {
response.writeHead(200, {'Content-Type': 'image/gif'})
fs.readFile(__dirname + '/public/logo.png', function(err, data) {
if(err) console.log(err)
response.end(data)
})
}
else if (request.url === '/'){
response.writeHead(200, {'Content-Type': 'text/html' })
fs.readFile(__dirname + '/public/index.html', function(err, data) {
if(err) console.log(err)
response.end(data)
})
}
})
const express = require('express')
const app = express()
app.use(express.static('./public'))
app.get('/', function(req, res, next) {
res.sendFile('./public/index.html')
})
app.listen(3000)
console.log('Express server is listening on port 3000')
- What tasks does this server perform?
- What parts of the process does Express help you with?
Lets practice building a simple server with express.
- Create a new folder called
express-lesson
- Run
npm init -y
- Install express dependencies:
npm i express body-parser nodemon morgan
- Create a file called
app.js
- Add the following script to
package.json
:"start": "nodemon app.js"
- Build the basic express server and open connections on port 3000.
- Copy the author data from above into
app.js
- Implement the following author routes in express:
- Get All
- Post
- Get One