Skip to content
Laviyx Learn

Try

Node.js and Express course menu

Beginner Node.js Interview Questions and Answers

Common Node.js and Express interview questions for freshers with clear answers and examples: what Node.js is, single-threaded non-blocking I/O, npm and package.json, modules, require vs import, process.env, the fs module, Express basics, middleware and status codes.

Beginner4 min readUpdated
On this page

These questions open most Node.js and back-end JavaScript screenings. Answer out loud first, then open the solution.

Node.js basics#

1. What is Node.js?#

Show solution

A JavaScript runtime built on Chrome's V8 engine that runs JavaScript outside the browser. It adds APIs for files, networking, processes and more, and uses non-blocking, event-driven I/O, which makes it well suited to web servers, APIs, real-time applications and developer tools.

2. Node.js is single-threaded. How can it handle many requests at once?#

Show solution

Your JavaScript runs on one thread, but slow I/O (disk, network, databases) is handed to the operating system and a background thread pool (libuv). While waiting, the thread keeps handling other requests; when an operation finishes, its callback is queued and the event loop runs it. So many requests are in progress concurrently, even though only one piece of JavaScript runs at a time. CPU-heavy work, however, blocks everyone, and belongs in worker threads.

3. What is the difference between blocking and non-blocking code?#

Show solution

Blocking code makes the program wait until an operation finishes (readFileSync). Non-blocking code starts the operation and continues; the result arrives later through a callback, promise or await (await readFile(...)). Servers should use non-blocking APIs so one slow operation does not delay other users.

4. What is npm, and what is package.json?#

Show solution

npm is Node's package manager: it installs packages from the npm registry and runs project scripts. package.json describes the project (name, version, type), its dependencies and devDependencies with allowed version ranges, and scripts such as start and test. package-lock.json records the exact installed versions so every install is identical.

5. require vs import?#

Show solution

require and module.exports belong to CommonJS, Node's original module system; modules load synchronously when require runs. import and export are ES modules, the JavaScript standard, enabled with "type": "module" or .mjs files; they support top-level await and are resolved before the code runs. New projects usually use ES modules.

6. How do you read environment variables, and why use them?#

Show solution

Through process.env.NAME (values are strings or undefined). Environment variables keep configuration and secrets (ports, database URLs, API keys) out of the code, so the same code runs in development and production with different settings. In development, node --env-file=.env app.js loads them from a .env file, which must not be committed.

7. What does this print?#

JavaScript
console.log("A");
setTimeout(() => console.log("B"), 0);
Promise.resolve().then(() => console.log("C"));
console.log("D");
Show solution
JavaScript
console.log("A");
setTimeout(() => console.log("B"), 0);
Promise.resolve().then(() => console.log("C"));
console.log("D");
Output
A
D
C
B

Synchronous code runs first (A, D), then promise callbacks (microtasks: C), then timers (B), even with a delay of 0.

Express#

8. What is Express, and why use it instead of the http module?#

Show solution

Express is a minimal web framework for Node.js. The http module can serve requests, but you must parse URLs, match routes, read bodies and set headers yourself. Express adds routing (app.get("/users/:id")), middleware, body parsing (express.json()), response helpers (res.json, res.status) and error handling, so APIs need far less code.

9. What is middleware?#

Show solution

A function (req, res, next) that runs during request handling. It can read or modify the request and response, end the response, or call next() to pass control on. Middleware runs in the order it is registered, and is used for logging, parsing bodies, authentication, CORS, validation and error handling.

10. What is the difference between req.params, req.query and req.body?#

Show solution
  • req.params: named parts of the path, e.g. /users/:id → req.params.id.
  • req.query: the query string, e.g. /products?page=2 → req.query.page.
  • req.body: the request body (for POST/PUT/PATCH), available after body-parsing middleware such as express.json().

Params and query values are always strings.

11. Which status codes would you return for create, not found, invalid input and server errors?#

Show solution

201 Created for a successful create (often with the new resource and a Location header), 404 Not Found for a missing resource, 400 Bad Request for invalid input (or 422 in some APIs), and 500 Internal Server Error for unexpected server failures. 401 means not logged in, 403 logged in but not allowed, and 204 No Content suits a successful delete.

Key takeaway#

Next: Intermediate Node.js Interview Questions.

Finished this lesson?