Skip to content

Instantly share code, notes, and snippets.

@jujhars13
Last active December 2, 2020 12:25
Show Gist options
  • Save jujhars13/e978bae3ead9d066d0da07119a26b10f to your computer and use it in GitHub Desktop.
Save jujhars13/e978bae3ead9d066d0da07119a26b10f to your computer and use it in GitHub Desktop.
Kubernetes 101 course - basic no-deps web server in Node
/**
* Plain Rest Web api, no external deps
* Nodejs > 14
*/
const http = require("http");
const {
port = 8080,
yourName = "Themba",
environment = "development",
} = process.env;
const requestHandler = (request, response) => {
const out = { message: "A Friendly JSON REST API", environment, yourName };
// have a "healthcheck" route that can be used to check service health
if (request.url === "/healthcheck" && request.method === "GET") {
out.message = "ok";
out.status = 200;
}
// "log" server access to stdout
console.log(
`${new Date()} [${request?.connection?.remoteAddress}]-[${
request?.headers?.["user-agent"]
}] ${yourName} accessed ${request.url} [${environment}]`
);
// send output as json
response.setHeader("Content-Type", "application/json");
response.statusCode = 200;
response.end(JSON.stringify(out));
};
const server = http.createServer(requestHandler);
server.listen(port, (err) => {
console.log(
`${environment} server listening on ${port}. Healthcheck on /healthcheck`
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment