Medium javascript

Simple HTTP response handler

Problem

Pseudo-code: if path is /health return 200 OK JSON.

Hints
  • Check req.url or path, set statusCode and end(JSON)

Your practice code

Ready — edit the code above and click Run.

Solution

const path = "/health";
if (path === "/health") {
  console.log(JSON.stringify({ status: 200, body: { ok: true } }));
} else {
  console.log(JSON.stringify({ status: 404, body: { error: "Not found" } }));
}

Try solving on your own first, then reveal the official answer.

Explanation

Express abstracts this—understand raw http.createServer callback for interviews.

Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details