Accessing API From Browser
The goal of this section is to learn how to create an API endpoint where the data provided is customizable based on user input, i.e., query parameters.
The Query String
For the user to get more custom data, we can accept query strings appended to our API endpoint. This appears in the req.query
object returned by express in the route handler callback.
If you want to make a query string required, you just need to add conditional logic that sends an error response if the query string isn't provided:
Note: Never have 2 res.send
calls execute. Every HTTP request can only have one response back. Express will throw an error otherwise.
Last updated