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
5
src/bin/server.ts
Normal file
5
src/bin/server.ts
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
import { app, port } from "../index.js";
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`demo-app listening on :${port}`);
|
||||
});
|
||||
|
|
@ -1,7 +1,33 @@
|
|||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { describe, it, expect } from "vitest";
|
||||
import request from "supertest";
|
||||
import { app } from "./index.js";
|
||||
import { greeting } from "./greeting.js";
|
||||
|
||||
test("greeting mentions the homelab", () => {
|
||||
assert.match(greeting(), /homelab/);
|
||||
describe("greeting", () => {
|
||||
it("mentions the homelab", () => {
|
||||
expect(greeting()).toMatch(/homelab/);
|
||||
});
|
||||
});
|
||||
|
||||
describe("GET /healthz", () => {
|
||||
it("returns 200 ok", async () => {
|
||||
const res = await request(app).get("/healthz");
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.text).toBe("ok");
|
||||
expect(res.type).toBe("text/plain");
|
||||
});
|
||||
});
|
||||
|
||||
describe("GET /", () => {
|
||||
it("returns service info", async () => {
|
||||
const res = await request(app).get("/");
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body).toMatchObject({
|
||||
service: "demo-app",
|
||||
message: "Hello from the homelab pipeline!",
|
||||
sha: expect.any(String),
|
||||
pod: expect.any(String),
|
||||
time: expect.any(String),
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
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