Update Dockerfile and package.json to include environment variables, expose port, and add server entry point; add server implementation in src/server.ts with health check and greeting endpoint.
Some checks failed
ci / build-test-pack (push) Failing after 1m21s
Some checks failed
ci / build-test-pack (push) Failing after 1m21s
This commit is contained in:
@@ -6,9 +6,12 @@ COPY package.json package-lock.json* tsconfig.json ./
|
||||
RUN npm install --no-audit --progress=false
|
||||
|
||||
COPY src ./src
|
||||
COPY .env* ./
|
||||
|
||||
RUN npm run build && npm pack
|
||||
|
||||
CMD ["node", "dist/index.js"]
|
||||
ENV PORT=3000
|
||||
EXPOSE 3000
|
||||
CMD ["node", "dist/server.js"]
|
||||
|
||||
|
||||
|
||||
3802
package-lock.json
generated
Normal file
3802
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
22
package.json
22
package.json
@@ -8,7 +8,9 @@
|
||||
"scripts": {
|
||||
"clean": "rimraf dist",
|
||||
"build": "npm run clean && tsc -p tsconfig.json",
|
||||
"test": "vitest run"
|
||||
"test": "vitest run",
|
||||
"start": "node dist/server.js",
|
||||
"dev": "tsx watch src/server.ts"
|
||||
},
|
||||
"files": [
|
||||
"dist",
|
||||
@@ -17,6 +19,22 @@
|
||||
"engines": {
|
||||
"node": ">=20"
|
||||
},
|
||||
"license": "MIT"
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"cors": "^2.8.5",
|
||||
"express": "^4.19.2",
|
||||
"helmet": "^7.1.0",
|
||||
"morgan": "^1.10.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/cors": "^2.8.17",
|
||||
"@types/express": "^4.17.21",
|
||||
"@types/morgan": "^1.9.7",
|
||||
"@types/node": "^20.14.12",
|
||||
"rimraf": "^5.0.7",
|
||||
"tsx": "^4.19.1",
|
||||
"typescript": "^5.5.4",
|
||||
"vitest": "^1.6.0"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
53
src/server.ts
Normal file
53
src/server.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import express, { Request, Response, NextFunction } from "express";
|
||||
import cors from "cors";
|
||||
import morgan from "morgan";
|
||||
import helmet from "helmet";
|
||||
import { Greeter } from "./core/Greeter";
|
||||
|
||||
const app = express();
|
||||
|
||||
// Middlewares
|
||||
app.use(helmet());
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
app.use(morgan("dev"));
|
||||
|
||||
// Health check
|
||||
app.get("/health", (_req: Request, res: Response) => {
|
||||
res.status(200).json({ status: "ok" });
|
||||
});
|
||||
|
||||
// Greeting endpoint
|
||||
app.get("/greet", (req: Request, res: Response) => {
|
||||
const name = (req.query.name as string) ?? "";
|
||||
const prefix = (req.query.prefix as string) ?? undefined;
|
||||
const greeter = new Greeter(prefix);
|
||||
const message = greeter.greet(name);
|
||||
res.json({ message });
|
||||
});
|
||||
|
||||
// 404 handler
|
||||
app.use((_req: Request, res: Response) => {
|
||||
res.status(404).json({ error: "Not Found" });
|
||||
});
|
||||
|
||||
// Error handler
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
app.use((err: unknown, _req: Request, res: Response, _next: NextFunction) => {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(err);
|
||||
res.status(500).json({ error: "Internal Server Error" });
|
||||
});
|
||||
|
||||
const port = Number(process.env.PORT ?? 3000);
|
||||
|
||||
if (require.main === module) {
|
||||
app.listen(port, () => {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(`Server listening on http://localhost:${port}`);
|
||||
});
|
||||
}
|
||||
|
||||
export default app;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user