Backend Flashcards

1
Q

What does app.use(express.json()) do in an Express.js application?

A

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

How do you define a route in Express.js that captures multiple parameters from the URL?

A

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.

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

What object in Express.js holds the values of route parameters, and how are they accessed?

A

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.

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

What is a practical use of multiple parameters in an Express.js route?

A

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.

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

What happens if res.send() is called multiple times in an Express.js route handler?

A

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

How can you prevent further code execution after sending a response in an Express.js route handler?

A

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.

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

What is a good practice when handling conditional responses in Express.js?

A

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.

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

What error might occur if you attempt to send a response after the response cycle has been completed in Express.js?

A

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.”

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

What does the dev script in a package.json file using nodemon src/server.js do?

A

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

How do you run the dev script from the package.json file?

A

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.

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