How to create a REST API Using Express Module #66 Flashcards
Pop quiz! What are the first 2 lines of code we always write when using Express.js?
- Require the library
- Declare a variable for our app and assign express( ) to it
const express = require('express') const app = express( )
What line of code is required in order to start up the express server instance?
app.listen(3000, ( ) => console.log(“Server is ready Captain!”)
You can choose any port you want as long as it isn’t in use. I used 3005 because somehow I had a port conflict.
Defining endpoints: which two params are accepted when writing endpoints?
The path to watch/listen and a callback function that is triggered when a new connection happens.
Example:
app.get(“/”, (req, res) => { /* Actions */ } )
What parameters are received when we run these express handlers?
app. get(‘/’, (req, res) => { /* / })
app. post(‘/’, (req, res) => { / / })
app. put(‘/’, (req, res) => { / / })
app. delete(‘/’, (req, res) => { / / })
app. patch(‘/’, (req, res) => { / */ })
( req, res ) =
Request, Response
They are variables and subject to whatever we want to call them. The order though needs to have the request first and I see no need to rename these to anything else that might be less readable.
How is the response object used when working with API’s
It is used to return information back to the user/client.
How are GET requests for the root directory handled?
They are handled automatically.
What’s the basic syntax for handling other directories or paths that you create?
app.get(“path”, (req, res) => { } )
Using Express, how can we send a response about the status of a request?
app.get(‘/dogs/”id”, (req, res) => {
res.status(404)
res.status(‘Dog not found’)
} )
The status and message can also be chained.
What language is used to serve up the response object?
JSON. Examples below:
app.get('/dogs', (req, res) => { const dogs = [] dogs.push({ name: 'Roger' }) dogs.push({ name: 'Syd' }) res.json(dogs) })
app.get('/dogs/:id', (req, res) => { const dogs = [{ id: 1, name: 'Roger' }]
const dog = dogs.filter(dog => dog.id === req.params.id) if (dog.length) { res.json(dog[0]) } else { res.status(404).send('File not found') } })
What does the basic syntax of a POST request look like in Express JS?
app.post(‘/’, (req, res) => { //do stuff })
What does the syntax look like in order to accept a JSON object
app.use(express.json( )) then you get the request body inside the request handler.
app.post('/dog', (req, res) => { const dog = { name: req.body.name, age: req.body.age } //we have the dog data, we can go on })
What is the most common way to send form data?
by sending URL encoded data. Example:
{ "name": "Roger", "age": 8 } Or when sent URL-encoded on the server-side looks like name=Roger&age=8.
How is a fetch request structured?
fetch(baseurl + '/dog', { "method": "POST", "headers": { "content-type": "application/x-www-form-urlencoded", }, "body": { "name": "Roger", "age": 8 } })
There is a much different way to set up a way to handle URL encoded data from a server. How is that set up?
app.use(express.urlencoded({ extended: true }))
The data then becomes available in the body property just like the JSON request like this:
app.post('/dog', (req, res) => { const dog = { name: req.body.name, age: req.body.age } //we have the dog data, we can go on })
The way it is “POSTed” is the same
What is the PUT method used for?
Udatating data on the server.
How is the most basic form of a PUT request structured?
Basically it’s app.put
except there is an ID in the URL
app.put(‘/dog/:id’, (req, res) => {})
Let’s say we want to update the data of something we already have on the server? What does that look like?
It looks much the same as a POST request. Example:
const express = require('express') const app = express()
app.use(express.json())
app.put('/dog/:id', (req, res) => { const dog = { name: req.body.name, age: req.body.age } //we have the new dog data, we can update it })
OR URL encoded data like this
const express = require('express') const app = express()
app.use(express.urlencoded({ extended: true }))
app.put('/dog/:id', (req, res) => { const dog = { name: req.body.name, age: req.body.age } //we have the new dog data, we can update it })
PATCH is also used to update data resources, but how does it differ from PUT?
Patch only partially updates a resource. For example we can update the age of somebody without touching the rest of the data object.
Here’s what it looks like:
app.patch(‘/dog/:id’, (req, res) => {})
In what way is PATCH the same as the PUT method?
You still need to include the /:id in the URL because we want to partially update a specific record. Here’s an example:
const express = require('express') const app = express()
app.use(express.json())
app.patch('/dog/:id', (req, res) => { const age = req.body.age //we have the new dog age, we can update it })
OR for URL encoded data
const express = require('express') const app = express()
app.use(express.urlencoded({ extended: true }))
app.patch('/dog/:id', (req, res) => { const age = req.body.age //we have the new dog age, we can update it })
DELETE is used for deleting resources, but how is it written?
const express = require('express') const app = express()
app.delete('/dog/:id', (req, res) => { //delete the dog with the specified `id` from our database }) A response with a 200 status code:
const express = require('express') const app = express()
app.delete('/dog/:id', (req, res) => { //delete the dog with the specified `id` from our database
// …
res.status(200).send(‘OK’)
})