refactor: migrate from node:http to Express with Express.js tests
This commit is contained in:
parent
6dd9c6e6ee
commit
1365bd7644
6 changed files with 2391 additions and 38 deletions
37
src/index.ts
37
src/index.ts
|
|
@ -1,32 +1,23 @@
|
|||
import { createServer } from "node:http";
|
||||
import express from "express";
|
||||
import { hostname } from "node:os";
|
||||
import { greeting } from "./greeting.js";
|
||||
|
||||
const app = express();
|
||||
const port = Number(process.env.PORT ?? 3000);
|
||||
const sha = process.env.GIT_SHA ?? "dev";
|
||||
|
||||
const server = createServer((req, res) => {
|
||||
if (req.url === "/healthz") {
|
||||
res.writeHead(200, { "content-type": "text/plain" });
|
||||
res.end("ok");
|
||||
return;
|
||||
}
|
||||
res.writeHead(200, { "content-type": "application/json" });
|
||||
res.end(
|
||||
JSON.stringify(
|
||||
{
|
||||
service: "demo-app",
|
||||
message: greeting(),
|
||||
sha,
|
||||
pod: hostname(),
|
||||
time: new Date().toISOString(),
|
||||
},
|
||||
null,
|
||||
2,
|
||||
) + "\n",
|
||||
);
|
||||
app.get("/healthz", (_req, res) => {
|
||||
res.type("text/plain").send("ok");
|
||||
});
|
||||
|
||||
server.listen(port, () => {
|
||||
console.log(`demo-app ${sha} listening on :${port}`);
|
||||
app.get("/", (_req, res) => {
|
||||
res.json({
|
||||
service: "demo-app",
|
||||
message: greeting(),
|
||||
sha,
|
||||
pod: hostname(),
|
||||
time: new Date().toISOString(),
|
||||
});
|
||||
});
|
||||
|
||||
export { app, port };
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue