Skip to content

Commit

Permalink
adding health check route (#749)
Browse files Browse the repository at this point in the history
  • Loading branch information
pacostas authored Jul 12, 2024
1 parent 2fedfba commit ce3575c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
20 changes: 18 additions & 2 deletions nodejs/no-package-manager/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(`<!DOCTYPE html>
<html>
<head>
Expand All @@ -10,8 +16,18 @@ const requestHandler = (request, response) => {
<body>
<img style="display: block; margin-left: auto; margin-right: auto; width: 50%;" src="https://paketo.io/images/paketo-logo-full-color.png"></img>
</body>
</html>`)
}
</html>`);
} 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)

Expand Down
14 changes: 13 additions & 1 deletion nodejs/npm/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(`<!DOCTYPE html>
<html>
Expand All @@ -15,4 +21,10 @@ app.get('/', (request, response) => {
</html>`);
});

app.listen(port);
app.get("/actuator/health", (request, response) => {
response.json({ status: "UP" });
});

app.listen(port, () => {
console.log(`App listening on port ${port}`);
});
14 changes: 13 additions & 1 deletion nodejs/yarn/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(`<!DOCTYPE html>
<html>
Expand All @@ -15,4 +21,10 @@ app.get('/', (request, response) => {
</html>`);
});

app.listen(port);
app.get("/actuator/health", (request, response) => {
response.json({ status: "UP" });
});

app.listen(port, () => {
console.log(`App listening on port ${port}`);
});

0 comments on commit ce3575c

Please sign in to comment.