Putting it together
- Node.js course
- Intermediate
- Lesson 69/116
- 6 min
Combine all parts into a working API.
Putting it together
The final lesson is integration: we combine all pieces (data model, Express, routes, middleware) into one working API. We also add an error middleware.
In Express, error middleware is defined with four arguments: (err, req, res, next). This signature tells Express it is an error handler. Errors thrown from routes or passed with next(err) land here. You return the error message to the client with res.status(500).json({ error: err.message }). 500 means “Server error”. This lesson is the last piece that makes the API production-ready, because errors are handled in a controlled way.
The error middleware signature
Express tells an error middleware from a normal one by the argument count. A normal middleware takes 3 arguments (req, res, next); an error middleware takes 4 (err, req, res, next).
If you write an error middleware with 3 arguments, Express treats it as a normal middleware and errors never reach it. So writing four arguments, with err first, is mandatory. When an error occurs, Express calls this middleware and passes the error object as err. err.message gives the error message. You set the server error code with res.status(500) and send the response with .json(). This way the app does not crash on unexpected errors.
Practice Add an error middleware
Define an error middleware. Make it a four-argument function (err, req, res, next). Send the error response with res.status(500).json({ error: err.message }). The middleware must come AFTER the routes. The self-request is ready.
Starter code
const books = [];
books.push({ id: 1, title: "Node.js" });
const express = require("express");
const app = express();
app.use(express.json());
app.use((req, res, next) => {
console.log(req.method, req.url);
next();
});
app.get("/books", (req, res) => {
res.json(books);
});
// Error middleware (4 arguments: err, req, res, next) — return 500 JSON
// write here
// Self-request (boilerplate)
const server = app.listen(3000, () => {
const http = require("http");
http.get("http://localhost:3000/books", (res) => {
let body = "";
res.on("data", (c) => body += c);
res.on("end", () => { console.log(body); server.close(); });
});
});Sample solution
app.use((err, req, res, next) => {
res.status(500).json({ error: err.message });
});Expected output
GET /books
[{"id":1,"title":"Node.js"}]
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Match each piece to its role
| Term | Meaning |
|---|---|
err |
Error object |
res.status(500) |
Server error code |
err.message |
Error message |
4 arguments |
Error middleware marker |
2 How many arguments does an Express error middleware take?
(err, req, res, next)- 4 Correct answer
- 3
- 2
- 5
Explanation: Error middleware takes 4 arguments; this signature marks it.
3 Set the status code
res.____(500).json({ error: err.message });
res.status(500).json({ error: err.message });
Explanation: res.status(500) sets the server error code.