Facebook Pixel

Node.js Server and HTTP Interview Questions With Answers

HTTP servers come up in Node.js interviews. Here are the common questions and how to answer them.

Node.js Server and HTTP Interview Questions With Answers

HTTP servers come up in Node.js interviews. Here are the common questions and how to answer them.

How do you create an HTTP server in Node.js?

Use require('http') and http.createServer with a request listener. The listener receives request and response. Call response.end to finish the response. Listen on a port with server.listen.

What is the difference between request and response in Node.js?

request (IncomingMessage) is the incoming HTTP request with url, method, headers, and body as a readable stream. response (ServerResponse) is what you send back: status code, headers, and body via writeHead, write, and end.

How do you read the request body in Node.js?

The request body is a readable stream. Listen to 'data' for chunks and 'end' for completion. For JSON, concatenate chunks and JSON.parse. Express's json middleware does this automatically.

What is middleware in Express?

A function that runs between the request and the response. It can modify the request, end the response, or call the next middleware. Auth, logging, and body parsing are common middleware.

How would you handle errors in a Node.js server?

Use try/catch in async route handlers, and a global error handler in Express with app.use(errorHandler). Unhandled errors crash the process, so always handle them.

How to Answer Well

Show you understand HTTP at the raw level, not just Express. Explain request/response streams, body parsing, and error handling. Interviewers want to see you know what Express does under the hood.

The Takeaway

Know how to create an HTTP server, request vs response, reading the request body, Express middleware, and error handling. Show you understand HTTP at the raw level, not just Express, to stand out.

Use require('http') and http.createServer with a request listener. The listener receives request and response. Call response.end to finish the response, and listen on a port with server.listen.

request (IncomingMessage) is the incoming HTTP request with url, method, headers, and body as a readable stream. response (ServerResponse) is what you send back: status code, headers, and body via writeHead, write, and end.

The request body is a readable stream. Listen to 'data' for chunks and 'end' for completion. For JSON, concatenate chunks and JSON.parse. Express's json middleware does this automatically.

A function that runs between the request and the response. It can modify the request, end the response, or call the next middleware. Auth, logging, and body parsing are common middleware.

Use try/catch in async route handlers, and a global error handler in Express with app.use(errorHandler). Unhandled errors crash the process, so always handle them.

Ready to master Node.js completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.