A small API that returns JSON, saves records, and handles bad input with clear errors is the base for most web apps, and you can build rest api with node.js and Express in one focused afternoon. This guide keeps the setup plain: one project folder, a few routes, a small data layer, and tests that catch the obvious breaks. You do not need a large framework or a paid service to start. You need Node 20 or newer, a code editor, and a terminal. By the end you will have a notes API with create, read, update, and delete routes, validation that rejects bad data, and error replies that tell the caller what went wrong. The same shape scales to a larger app later, because each part has one job and lives in its own file.
What you will build
Before you write code, set the shape of the project. A clear node.js api project structure saves time later, when a new route or a new team member joins. Keep the entry point thin, keep routes in one folder, keep the data logic in another, and keep validation close to the route that needs it. If a file grows past a few hundred lines, split it. A file that does routing, validation, and storage at once is hard to test and hard to read, and it will slow you down the next time you touch it.
notes-api/
src/
index.js
app.js
routes/notes.js
store/notesStore.js
middleware/errorHandler.js
package.json
How to build rest api with node.js from a clean folder
mkdir notes-api && cd notes-api npm init -y npm install express node src/index.js
Put the listen call in index.js and the Express setup in app.js. That split lets tests import the app without starting a server. Add JSON parsing with express.json, then add a request log line in development so you can see each call. Keep CORS off until you need a browser client from another origin, then add it with a clear list of allowed origins. If you plan to containerize a Node.js app later, this split also helps, because the container can run the same entry point with an environment port.
Routing that stays easy to read
Routes should read like a list of actions. Use a router for notes, mount it at /api/notes, and keep each handler short. A handler reads input, calls the store, and sends a reply. It should not run raw database queries or build long SQL strings. That work belongs in the store or in a small helper. This express routing middleware guide pattern keeps the route file short enough to scan in one view, and it makes a bug easy to find, because each file has one clear job.
const router = require("express").Router();
router.get("/", (req, res) => {
res.json(store.list());
});
router.get("/:id", (req, res) => {
const note = store.get(req.params.id);
if (!note) return res.status(404).json({ error: "Note not found" });
res.json(note);
});
router.post("/", (req, res) => {
const note = store.create(req.body);
res.status(201).json(note);
});
Use plural nouns for collections and an id in the path for one item. Return 201 when a create works, 200 for a read or update, and 204 with no body when a delete works. Send the created item back in the create reply so the client does not need a second call to learn the new id. Keep the JSON shape the same across routes: a note object for one item, and an array for a list. A client that has to guess the shape on each route will break in small, annoying ways.
Middleware that does real work
Do not put business logic in middleware. Middleware should check, log, or block, then pass on. When middleware starts creating records or sending mail, the route becomes hard to test, because the side effect hides in a layer the test did not expect. Keep a short list of the middleware your app uses, in the order it runs, at the top of app.js or in a comment. That list is a map for the next person who reads the code, and it will save them from guessing why a request stops before it reaches a route.
Data, validation, and error handling
Good rest api error handling uses one place for errors. Add an error handler at the end of the chain that logs the error, picks a status code, and sends a JSON object with a short message. Keep the stack trace out of the reply in production, and send a generic message for a 500 error. For a 404, send a short “not found” reply. Test each case with curl so you know the exact reply shape before a client depends on it. If your API needs keys, read how to store API keys and secrets safely before you hard-code anything in the source.
curl -X POST http://localhost:3000/api/notes
-H "Content-Type: application/json"
-d '{"title":"Buy milk","body":"Oat milk, two cartons"}'
curl http://localhost:3000/api/notes
Testing and docs before you ship
Docs matter as soon as a second person uses the API. Write a short README with the base URL, the list of routes, an example request, and an example reply for each route. Add the error shapes too, so a client knows what a 400 or 404 looks like. For a longer guide, read how to document your APIs like a pro and pick the parts that fit a small team. A good doc set also lists the tools you use to test calls, and a roundup of the best API testing tools in 2026, Postman, Insomnia, and Bruno can help you pick one that fits your flow.
FAQ
Do I need a database for my first API?
No. Start with an in-memory store or a JSON file, get the routes and validation right, then move to a database when you need data that survives a restart. The route code should not change much when the store changes, if you kept the store behind small functions.
Should I use TypeScript from the start?
Plain JavaScript is fine for a first API and for a small team. Add TypeScript when the project grows, when several people edit the same files, or when type errors start costing real time. A move to TypeScript is easier when the routes are already split into small files.
How do I know my error replies are good enough?
Send a bad request with curl and read the reply. A good reply names the problem, names the field when one is wrong, and uses the right status code. If a new client can fix the call from the reply alone, the error handling is doing its job.
Ship the notes API, then grow it one piece at a time. Add auth for private notes, add a database for data that must last, and add pagination when the list grows past a few dozen items. Each addition should land as a small change with a test, not as a large rewrite. That habit is what turns a starter project into an API you can trust in production.