CodeBlanks Logo Icon CodeBlanks

Putting it together

Combine all layers into a working mini service.

Combining

In the final lesson we combine all pieces into a full application skeleton: Express API, database, WebSocket and security. This is the full skeleton of an advanced Node.js application.

The skeleton is this: express for the app, db for the database, io for the WebSocket, app.use for security. All work together in a single Node.js process. In production, authentication, logging, monitoring and more routes are added on top of this skeleton. But the core flow is the same: Express routes look at the database, the WebSocket looks at real-time events, security looks at all requests. This separation makes the system scalable and easy to maintain.

The full application skeleton

The full skeleton: Express app, root route, DB route, WebSocket object, security middleware and a /health status route. All come together in one file; in production they are split into modules.

Flow: const express = require("express"); const app = express(); app.use((req, res, next) => next()); const db = { ready: true }; app.get("/db", (req, res) => res.json({ db: db.ready })); const io = { on: () => {} }; app.get("/health", (req, res) => res.json({ status: "system ready" })); console.log("System ready");. This skeleton is the combination of all lessons. In production app, db and io become separate modules, each in its own file. But the main flow is the same. This structure is the foundation of advanced Node.js applications.

Practice Add the /health route and final log

Add an app.get("/health", (req, res) => res.json({ status: "system ready" })) route to the skeleton below. Print "System ready" with console.log.

Starter code

index.js
const express = require("express");
const app = express();
app.get("/", (req, res) => res.json({ status: "ok" }));
const db = { ready: true };
app.get("/db", (req, res) => res.json({ db: db.ready }));
const io = { on: () => {} };
app.use((req, res, next) => { next(); });
// add /health route
// write here
// Print "System ready"
// write here

Sample solution

index.js
const express = require("express");
const app = express();
app.get("/", (req, res) => res.json({ status: "ok" }));
const db = { ready: true };
app.get("/db", (req, res) => res.json({ db: db.ready }));
const io = { on: () => {} };
app.use((req, res, next) => { next(); });
app.get("/health", (req, res) => res.json({ status: "system ready" }));
console.log("System ready");

Expected output

System ready

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What four pieces does the full skeleton combine?

index.js
Express + DB + WebSocket + Security
  • Only require
  • Only console.log
  • Express, DB, WebSocket and security Correct answer
  • Only app.get

Explanation: The right answer is: Express, DB, WebSocket and security combine.

2 Match each piece to its role

Term Meaning
express API framework
db Stores data
io Real-time
app.use Security middleware

3 Complete the app.get line

Question
app.get("____", (req, res) => res.json({ status: "ok" }));
Answer
app.get("/health", (req, res) => res.json({ status: "ok" }));

Explanation: The /health route returns the system state.

All lessons in Advanced