Q1. What properties are commonly used on the request object?
Important req properties:
•
•
•
•
•
•
•
•
•
req.params (route parameters)•
req.query (query string)•
req.body (parsed body)•
req.headers (HTTP headers)•
req.method (HTTP method)•
req.url (URL)•
req.cookies (cookies with cookie-parser)•
req.ip (client IP)Q2. What methods are commonly used on the response object?
Common res methods:
•
•
•
•
•
•
•
•
•
•
res.send()•
res.json()•
res.status()•
res.redirect()•
res.render() (for templates)•
res.set() (set headers)•
res.cookie() (set cookies)•
res.end() (end response)•
res.download() (send file for download)Q3. How do you set HTTP status codes in Express?
Use
Common codes:
• 200 OK
• 201 Created
• 400 Bad Request
• 401 Unauthorized
• 403 Forbidden
• 404 Not Found
• 500 Internal Server Error
res.status(code). It's chainable: res.status(201).json({...}).Common codes:
• 200 OK
• 201 Created
• 400 Bad Request
• 401 Unauthorized
• 403 Forbidden
• 404 Not Found
• 500 Internal Server Error
Q4. How do you access request headers?
Through
Example:
Header names are lowercase.
You can also use
For cookies, you need cookie-parser middleware, then
req.headers object.Example:
const authHeader = req.headers['authorization'];Header names are lowercase.
You can also use
req.get(headerName).For cookies, you need cookie-parser middleware, then
req.cookies.Q5. What's the difference between res.send() and res.end()?
Use
res.send() automatically sets appropriate Content-Type and ends the response. It can send objects, strings, buffers.res.end() simply ends the response without sending data.Use
res.send() for most cases; res.end() is for when you just want to end without data.