AceDevHub
Intermediate Node.js Interview QuestionsIntermediateConcept

Node.js · Question 67

What are REST API basics in Node.js?

Direct answer

REST maps resources to URLs and HTTP verbs — GET read, POST create, PUT/PATCH update, DELETE remove; use nouns (/interviews/topics), status codes, JSON bodies, pagination query params, and idempotent design; implement with Fastify routes and validation schemas.

AceDevHub's API exposes resource-oriented endpoints — GET /interviews/topics/:slug, structured envelopes, consistent errors — not RPC-style action URLs for core CRUD.

VerbActionExample
GETRead collection or itemGET /interviews/topics/nodejs-interview-questions
POSTCreatePOST /courses/waitlist
PUT/PATCHUpdatePATCH /admin/courses/:id
DELETERemoveDELETE /sessions/:id
201 + LocationCreated resourcePOST returns new id
  • Status codes — 200 OK, 201 Created, 400 validation, 401 auth, 404 missing, 409 conflict, 500 server.
  • Pagination — ?page=&limit= (Q30); Link header or meta in envelope.
  • Idempotency — PUT same body twice same result; POST creates duplicates without Idempotency-Key.
  • Validation — Fastify JSON schema on body/query/params before service layer.
rest-routes.mjs
fastify.get("/interviews/topics/:slug", {
  schema: {
    params: { type: "object", properties: { slug: { type: "string" } }, required: ["slug"] },
  },
}, async (req, reply) => {
  const topic = await interviewsService.getTopicPage(req.params.slug);
  if (!topic) return reply.code(404).send({ error: { code: "NOT_FOUND" } });
  return reply.send({ data: { page: topic } });
});