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.