Defining RESTful Routes Flashcards
What is a get request?
Get requests are used to get information and when we submit, if we have data that is being sent alongside it is in the query string (url). Limited amount of data can be sent. Should be no side effects on back end like things being created/updated.
Submit
Now if we add some data in the form on browser we’ll get information in query string.
What is a post request?
Posting data to the server. Used if we have information that is important that we need to send as part of the request and it won’t be sent in query string, instead it will be sent in the request body. Means we can send more data and of any type like json.
How would we use post in Express?
app.post(‘/tacos’, (req, res) => {res.send(“Post /tacos response”) }
How would we extract data from the request body of a post and do something with it?
req includes a property called req.body. Contains key value pairs of data submitted in the request body. By default is undefined. It is populated when we use body-parsing middleware such as express.json() or express.urlencoded().
app. use(express.urlencoded({extended:true}
app. use is a function that runs on every single request.
We can destructure from it as well.
app.post(‘/tacos’, (req, res) => {
const {meat, qty } = req.body
res.send(Ok here are your ${qty} ${meat} tacos.
) }
What is REST?
Basically set of guidelines for how a client-server should communicate with each other.