AceDevHub
Intermediate Node.js Interview QuestionsIntermediateConcept

Node.js · Question 43

What are the basics of Express.js?

Direct answer

Express is a minimal Node.js web framework — app.get/post for routes, app.use for middleware, Router for modular paths, and res.json/status for responses; AceDevHub uses Fastify instead, but Express patterns appear in most Node interview loops.

Express sits on Node's http module and adds routing, middleware composition, and response helpers — the de facto teaching stack even when production code picks Fastify or Koa.

APIRole
express()Create app instance
app.use(mw)Register middleware for all/mounted paths
app.get('/path', handler)HTTP verb + route handler
express.Router()Sub-router mounted at prefix
res.json({})Send JSON with Content-Type
next()Pass control in middleware chain
  • Static files — express.static('public') for assets.
  • Error middleware — four-arg (err, req, res, next) handler last in stack.
  • Fastify contrast — schema-first validation, faster JSON serialization, plugin encapsulation.
express-basics.mjs
import express from "express";

const app = express();
app.use(express.json());

const interviews = express.Router();
interviews.get("/:slug", (req, res) => {
  res.json({ slug: req.params.slug, topic: "nodejs-interview-questions" });
});

app.use("/interviews", interviews);

app.use((err, req, res, next) => {
  console.error(err);
  res.status(500).json({ error: "Internal Server Error" });
});

app.listen(4000);