From ce3575c68d41a57a2e77638a84d53e39d6cbafa1 Mon Sep 17 00:00:00 2001 From: Costas Papastathis Date: Fri, 12 Jul 2024 15:21:16 +0300 Subject: [PATCH] adding health check route (#749) --- nodejs/no-package-manager/server.js | 20 ++++++++++++++++++-- nodejs/npm/server.js | 14 +++++++++++++- nodejs/yarn/server.js | 14 +++++++++++++- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/nodejs/no-package-manager/server.js b/nodejs/no-package-manager/server.js index 6c2b308c..dd78b39c 100644 --- a/nodejs/no-package-manager/server.js +++ b/nodejs/no-package-manager/server.js @@ -2,6 +2,12 @@ const http = require('http') const port = process.env.PORT || 8080 const requestHandler = (request, response) => { + + const requestTime = new Date(Date.now()).toString(); + console.log(request.method, request.headers.host, request.url, requestTime); + + if (request.url === "/") { + response.writeHead(200, { "Content-Type": "text/html" }); response.end(` @@ -10,8 +16,18 @@ const requestHandler = (request, response) => { -`) -} +`); + } else if ( + request.url === "/actuator/health" && + request.method === "GET" + ) { + response.writeHead(200, { "Content-Type": "application/json" }); + response.end(JSON.stringify({ status: "UP" })); + } else { + response.writeHead(404, { "Content-Type": "text/plain" }); + response.end("Not Found"); + } +}; const server = http.createServer(requestHandler) diff --git a/nodejs/npm/server.js b/nodejs/npm/server.js index 4eb743fd..c9538ea7 100644 --- a/nodejs/npm/server.js +++ b/nodejs/npm/server.js @@ -3,6 +3,12 @@ const port = process.env.PORT || 8080; const app = express(); +app.use((request, _, next) => { + const requestTime = new Date(Date.now()).toString(); + console.log(request.method, request.hostname, request.path, requestTime); + next(); +}); + app.get('/', (request, response) => { response.send(` @@ -15,4 +21,10 @@ app.get('/', (request, response) => { `); }); -app.listen(port); +app.get("/actuator/health", (request, response) => { + response.json({ status: "UP" }); +}); + +app.listen(port, () => { + console.log(`App listening on port ${port}`); +}); diff --git a/nodejs/yarn/server.js b/nodejs/yarn/server.js index 4eb743fd..d5546d25 100644 --- a/nodejs/yarn/server.js +++ b/nodejs/yarn/server.js @@ -3,6 +3,12 @@ const port = process.env.PORT || 8080; const app = express(); +app.use((request, _, next) => { + const requestTime = new Date(Date.now()).toString(); + console.log(request.method, request.hostname, request.path, requestTime); + next(); +}); + app.get('/', (request, response) => { response.send(` @@ -15,4 +21,10 @@ app.get('/', (request, response) => { `); }); -app.listen(port); +app.get("/actuator/health", (request, response) => { + response.json({ status: "UP" }); +}); + +app.listen(port, () => { + console.log(`App listening on port ${port}`); +}); \ No newline at end of file