Interview Q&A / Node.js

INTERVIEW Q&A · 68 QUESTIONS

Node.js interview questions

The event loop on the server, streams, modules and building APIs with Express.

  1. How do you create a basic HTTP server in Node.js without Express?

    Use the built-in http module: http.createServer takes a handler with request and response objects, and listen starts it on a port. You set status and headers on the response and finish it with end.

    BeginnerQuick answer1 min read
  2. What is the difference between PUT and PATCH in a REST API?

    PUT replaces the whole resource with the body you send, so missing fields are cleared. PATCH changes only the fields you send. PUT is idempotent by definition; PATCH is not guaranteed to be.

    BeginnerQuick answer1 min read
  3. What is the difference between route parameters and query strings in Express?

    Route parameters are part of the path, like /users/42, and identify a specific resource, read from req.params. Query strings follow a question mark, like ?page=2, and filter or tweak a request, read from req.query.

    BeginnerQuick answer1 min read
  4. Which HTTP status codes should a REST API return for create, validation errors, missing login and forbidden access?

    201 Created for a successful create, 400 for bad input (or 422 for failed validation), 401 when the user is not authenticated, 403 when authenticated but not allowed, 404 for a missing resource, and 500 for server faults.

    BeginnerQuick answer1 min read
  5. Why does req.body come out undefined in Express, and what does express.json do?

    Express does not read request bodies by itself. express.json() is middleware that parses JSON bodies with a JSON content type and places the result on req.body. Without it, req.body is undefined.

    BeginnerQuick answer1 min read