REST resources and methods
Local practice: Node.js 22.16.0 on Linux x64; proposed 26.13.1 baseline remains untested. Save each example separately as main.mjs in a disposable directory. No packages or external services required. HTTP examples bind only to loopback and shut down after a finite scenario. Static Node.js code, not verified ByteMind execution.
Model a catalog read
import { createServer } from 'node:http';import { once } from 'node:events';async function withServer(handler, use) { const server = createServer({ headersTimeout: 2000, requestTimeout: 2000 }, handler); server.listen(0, '127.0.0.1'); await once(server, 'listening'); try { await use(`http://127.0.0.1:${server.address().port}`); } finally { await new Promise(resolve => { server.close(resolve); server.closeAllConnections(); }); }}const localFetch = (url, options = {}) => fetch(url, { ...options, redirect: 'error', signal: options.signal ?? AbortSignal.timeout(2000)});const item={id:1,title:'Notebook'};await withServer((q,r)=>{if(q.method==='GET'&&q.url==='/catalog/1'){r.setHeader('content-type','application/json');r.end(JSON.stringify(item));}else{r.statusCode=404;r.end();}},async base=>{const r=await localFetch(base+'/catalog/1');console.log(r.status,(await r.json()).id);});Run:
node main.mjsExpected stdout:
200 1Exit status: 0. Expected stderr: empty.
How it works
A resource is the thing addressed; JSON is one possible representation. GET is intended for retrieval, POST commonly submits a creation request to a collection, PUT replaces a resource representation, PATCH applies a defined partial change and DELETE requests removal. REST is a broader architectural style, not a synonym for any JSON API. A successful creation can return 201 and a Location header. A deletion can return 204 without a body. The examples are in-memory contracts, not durable storage.
Creation response contract
import { createServer } from 'node:http';import { once } from 'node:events';async function withServer(handler, use) { const server = createServer({ headersTimeout: 2000, requestTimeout: 2000 }, handler); server.listen(0, '127.0.0.1'); await once(server, 'listening'); try { await use(`http://127.0.0.1:${server.address().port}`); } finally { await new Promise(resolve => { server.close(resolve); server.closeAllConnections(); }); }}const localFetch = (url, options = {}) => fetch(url, { ...options, redirect: 'error', signal: options.signal ?? AbortSignal.timeout(2000)});await withServer((q,r)=>{if(q.method==='POST'&&q.url==='/catalog'){r.statusCode=201;r.setHeader('location','/catalog/2');r.end();}else{r.statusCode=404;r.end();}},async base=>{const r=await localFetch(base+'/catalog',{method:'POST'});await r.text();console.log(r.status,r.headers.get('location'));});Run:
node main.mjsExpected stdout:
201 /catalog/2Exit status: 0. Expected stderr: empty.
Common mistake
Do not design GET endpoints that perform requested state changes. A method’s semantics matter even when the server could technically implement another behavior.
Practice
Design a PUT /catalog/1 endpoint that replaces a title and a DELETE /catalog/1 endpoint. Describe the response for an unknown item.
Further reading: Authoritative source 1 · Authoritative source 2. Internal reference routes remain proposed until repository discovery.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
REST resources and methods — knowledge check
Apply the concepts and predict the behavior taught in this lesson.
Which method is intended for retrieval?
Which response indicates creation and can identify the new resource with Location?
What content should a 204 response carry?