Backend Flashcards
What does app.use(express.json()) do in an Express.js application?
app.use(express.json()) is middleware that parses incoming JSON payloads in Express.js, allowing access to req.body as a JavaScript object. It’s essential for handling JSON data in API requests, ensuring the data is properly formatted and accessible in route handlers.
How do you define a route in Express.js that captures multiple parameters from the URL?
Define multiple parameters in the URL path by prefixing them with a colon :, like /hello/:name/:age. These are accessible in the route handler via req.params.
What object in Express.js holds the values of route parameters, and how are they accessed?
The req.params object holds the values of route parameters. They are accessed in a route handler by referencing the parameter name, like req.params.name and req.params.age, corresponding to the route definition.
What is a practical use of multiple parameters in an Express.js route?
Multiple parameters are used to create dynamic responses based on user input, such as personalizing greetings, filtering data, or handling specific user requests efficiently by passing variable data through the URL.
What happens if res.send() is called multiple times in an Express.js route handler?
Calling res.send() multiple times in the same request-response cycle will result in an error because the response cycle can only be concluded once. Additional attempts to send a response will throw an error about setting headers after they have been sent.
How can you prevent further code execution after sending a response in an Express.js route handler?
You can prevent further code execution after sending a response by using the return statement with res.send(). This ensures that the function exits immediately after the response is sent, avoiding any further operations or response attempts.
What is a good practice when handling conditional responses in Express.js?
A good practice is to use conditional blocks to manage different response paths and to use return res.send() within these blocks to ensure that no further response attempts are made after the response cycle is completed.
What error might occur if you attempt to send a response after the response cycle has been completed in Express.js?
If you attempt to send a response after the response cycle has been completed, Express.js will throw an error indicating that headers cannot be set after they are sent to the client, commonly known as “Cannot set headers after they are sent.”
What does the dev script in a package.json file using nodemon src/server.js do?
The dev script starts nodemon to monitor the src/server.js file for changes, automatically restarting the server whenever the file or its dependencies are updated.
How do you run the dev script from the package.json file?
You run the dev script by executing npm run dev in the terminal. This command activates nodemon to monitor and restart the Node.js server automatically during development.