0% found this document useful (0 votes)
4 views4 pages

Express

The document contains 20 multiple-choice questions about Express.js, covering topics such as routing, middleware, and response methods, along with their correct answers. Additionally, it includes coding requirements for creating routes and middleware in Express. The questions and coding tasks aim to test knowledge and practical skills in using Express.js effectively.

Uploaded by

malakasran339
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Express

The document contains 20 multiple-choice questions about Express.js, covering topics such as routing, middleware, and response methods, along with their correct answers. Additionally, it includes coding requirements for creating routes and middleware in Express. The questions and coding tasks aim to test knowledge and practical skills in using Express.js effectively.

Uploaded by

malakasran339
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Express lecture

20 Multiple-Choice Questions (MCQs) with Answers

1. What is the purpose of app.get() in Express?


a. To handle HTTP POST requests
b. To handle HTTP GET requests
c. To define middleware functions
d. To redirect routes
Answer: b
2. What method is used to send a JSON response in Express?
a. res.send()
b. res.json()
c. res.write()
d. res.redirect()
Answer: b
3. What does req.params represent?
a. Query parameters in the URL
b. Dynamic route parameters
c. Data from a POST request
d. Middleware properties
Answer: b
4. Which HTTP method is used for updating data?
a. GET
b. POST
c. PUT
d. DELETE
Answer: c
5. What is the default port number for an Express app?
a. 3000
b. 8080
c. 5000
d. 80
Answer: a
6. How do you apply middleware globally in Express?
a. app.get()
b. app.use()
c. app.post()
d. app.route()
Answer: b
7. What does res.redirect('/home') do?
a. Ends the response process
b. Redirects the user to /home
c. Renders the /home view
d. Sends a 404 response
Answer: b
8. How do you access query parameters in Express?
a. req.query
b. req.params
c. req.body
d. req.header
Answer: a
9. What is the purpose of next() in middleware?
a. Ends the response cycle
b. Passes control to the next middleware
c. Logs the request
d. Redirects the user
Answer: b
10. What will this route match? app.get('/user/:id', ...)
a. /user?id=5
b. /user/5
c. /user?id=5&name=John
d. /user
Answer: b
11. Which of the following is a valid response method in Express?
a. res.log()
b. res.status()
c. res.query()
d. res.get()
Answer: b
12. What type of data does req.body hold?
a. Query parameters
b. JSON or form data from POST requests
c. Headers sent by the client
d. Dynamic route parameters
Answer: b
13. Which function is used to start the Express server?
a. app.listen()
b. app.start()
c. app.begin()
d. app.run()
Answer: a
14. What is the output of res.json({ name: 'John' })?
a. Sends a text response
b. Sends a JSON response
c. Sends an HTML response
d. Sends an error
Answer: b
15. What does app.all() do?
a. Matches any HTTP method for a given path
b. Applies middleware to all routes
c. Ends the response process
d. Matches only GET requests
Answer: a
16. What happens if you don’t call next() in middleware?
a. The request continues normally
b. The request hangs indefinitely
c. The server crashes
d. A 404 error is sent
Answer: b
17. What is the purpose of res.status(404)?
a. To end the response cycle
b. To set the HTTP status code to 404
c. To redirect the user
d. To send JSON data
Answer: b
18. Which method handles dynamic routing?
a. app.dynamic()
b. req.params
c. app.get()
d. res.send()
Answer: c
19. How do you handle 404 errors in Express?
a. app.notFound()
b. res.error()
c. Using a catch-all middleware
d. Redirecting to /error
Answer: c
20. Which of these is not a valid HTTP method in Express?
a. GET
b. PATCH
c. OPTIONS
d. QUERY
Answer: d

Questions with Coding Requirements

1. Write a route to handle a GET request to /welcome and send the response
"Welcome to Express!".

javascript
CopyEdit
app.get('/welcome', (req, res) => {
res.send('Welcome to Express!');
});

2. Write a dynamic route that extracts a userId from the URL /user/:id and sends it
back in the response.
javascript
CopyEdit
app.get('/user/:id', (req, res) => {
const userId = req.params.id;
res.send(`User ID: ${userId}`);
});

3. Create middleware that logs the requested URL and then passes control to the next
middleware.

javascript
CopyEdit
app.use((req, res, next) => {
console.log(`Request URL: ${req.url}`);
next();
});

4. Write a route to handle a POST request to /submit and respond with "Data
received".

javascript
CopyEdit
app.post('/submit', (req, res) => {
res.send('Data received');
});

5. Create a middleware that checks if a query parameter auth=true is present. If not,


respond with "Unauthorized".

javascript
CopyEdit
app.use((req, res, next) => {
if (req.query.auth !== 'true') {
res.status(401).send('Unauthorized');
} else {
next();
}
});

You might also like