Start learning free

LESSON 52 OF 58 · FULL-STACK: SHIPPING IT

Project: Ready to Release?

Practice

When you release a new version of the shop, the host starts it beside the old one and asks it GET /health before sending it any visitors. A health check answers whether this server can really serve: here, whether the database answers and whether the built storefront, client/dist/index.html, is there. If the status is 200, the host switches; otherwise it keeps the old release running.

The host reads only the status. The JSON is for the people who look: it names each part and says "ok" or what is wrong. When a part fails, the answer is 503 Service Unavailable: the server is up, but it is not ready to serve.

A query on a closed database throws, so a try/catch turns the error into a value, and fs.existsSync(path) says whether a file is there. Here is a status route from another app, which reports its uploads folder (it always answers 200, so a host could not use it):

app.get("/status", (req, res) => {
  const status = { uploads: "ok" };
  if (!fs.existsSync("uploads")) {
    status.uploads = "missing";
  }
  res.json(status);
});

The check asks /health, then closes the database, then hides index.html. Your app must answer:

GET /health                         →  200  {"client": "ok", "database": "ok"}
(database closed)     GET /health   →  503  {"client": "ok", "database": "down"}
(index.html missing)  GET /health   →  503  {"client": "missing", "database": "ok"}

Your task: in server/app.js, the route GET /health is given with an empty try/catch, and dist is the path of client/dist. Build the health object, try the query SELECT 1, look for index.html, and answer 503 with health when a part fails, 200 otherwise.


Assignment
Follow the Coding Tutorial to write the shop's health check, GET /health, that checks the database and the built storefront.


Hint
Start both parts at "ok" and let each check change only its own part. Choose the status last, after both checks, and put return before res.status(503) so that res.json does not run as well.


STEP 1 OF 8
    Ctrl + Enter to check