27 lines
864 B
JavaScript
27 lines
864 B
JavaScript
const { createServer } = require("node:http");
|
|
|
|
const port = Number(process.env.PORT || 8080);
|
|
|
|
function sendJson(response, status, body) {
|
|
response.writeHead(status, { "content-type": "application/json; charset=utf-8" });
|
|
response.end(JSON.stringify(body));
|
|
}
|
|
|
|
const server = createServer((request, response) => {
|
|
const pathname = new URL(request.url || "/", "http://localhost").pathname;
|
|
|
|
if (request.method === "GET" && (pathname === "/" || pathname === "/health")) {
|
|
return sendJson(response, 200, { status: "ok", service: "be" });
|
|
}
|
|
|
|
if (request.method === "GET" && pathname === "/api/hello") {
|
|
return sendJson(response, 200, { service: "be", message: "Hello from the backend" });
|
|
}
|
|
|
|
return sendJson(response, 404, { error: "Not found" });
|
|
});
|
|
|
|
server.listen(port, "0.0.0.0", () => {
|
|
console.log(`BE listening on port ${port}`);
|
|
});
|