How to create a REST API Using Express Module #66 Flashcards

1
Q

Pop quiz! What are the first 2 lines of code we always write when using Express.js?

A
  1. Require the library
  2. Declare a variable for our app and assign express( ) to it
const express = require('express')
const app = express( )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What line of code is required in order to start up the express server instance?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Defining endpoints: which two params are accepted when writing endpoints?

A

The path to watch/listen and a callback function that is triggered when a new connection happens.

Example:
app.get(“/”, (req, res) => { /* Actions */ } )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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) => { /
*/ })

A

( 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How is the response object used when working with API’s

A

It is used to return information back to the user/client.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How are GET requests for the root directory handled?

A

They are handled automatically.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What’s the basic syntax for handling other directories or paths that you create?

A

app.get(“path”, (req, res) => { } )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Using Express, how can we send a response about the status of a request?

A

app.get(‘/dogs/”id”, (req, res) => {
res.status(404)
res.status(‘Dog not found’)
} )

The status and message can also be chained.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What language is used to serve up the response object?

A

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')
  }
})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does the basic syntax of a POST request look like in Express JS?

A

app.post(‘/’, (req, res) => { //do stuff })

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does the syntax look like in order to accept a JSON object

A

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
})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the most common way to send form data?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How is a fetch request structured?

A
fetch(baseurl + '/dog', {
  "method": "POST",
  "headers": {
    "content-type": "application/x-www-form-urlencoded",
  },
  "body": {
    "name": "Roger",
    "age": 8
  }
})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

There is a much different way to set up a way to handle URL encoded data from a server. How is that set up?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the PUT method used for?

A

Udatating data on the server.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How is the most basic form of a PUT request structured?

A

Basically it’s app.put
except there is an ID in the URL
app.put(‘/dog/:id’, (req, res) => {})

17
Q

Let’s say we want to update the data of something we already have on the server? What does that look like?

A

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
})
18
Q

PATCH is also used to update data resources, but how does it differ from PUT?

A

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) => {})

19
Q

In what way is PATCH the same as the PUT method?

A

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
})
20
Q

DELETE is used for deleting resources, but how is it written?

A
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’)
})