DEV Community

buildlogmmd
buildlogmmd

Posted on • Edited on

Setup Express and add /health route

Commit

chore: setup Express and add /health route
Enter fullscreen mode Exit fullscreen mode

Commands

npm install express
npm install --save-dev @types/express
Enter fullscreen mode Exit fullscreen mode

Files

๐Ÿ“„ src/app.ts

import express from "express";

const app = express();

app.get("/health", (_req, res) => {
  res.status(200).json({ status: "ok" });
});

export default app;
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“„ src/server.ts

import app from "./app";

const PORT = process.env.PORT || 3001;

app.listen(PORT, () => {
  console.log(`Server running at http://localhost:${PORT}`);
});
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“„ Update package.json (in "scripts")

"scripts": {
  "dev": "tsx watch src/server.ts"
}
Enter fullscreen mode Exit fullscreen mode

Verification

  1. Run the development server:
npm run dev
Enter fullscreen mode Exit fullscreen mode
  1. Open your browser or use a tool like Postman:
GET http://localhost:3001/health
Enter fullscreen mode Exit fullscreen mode
  1. Expected response:
{ "status": "ok" }
Enter fullscreen mode Exit fullscreen mode

Next step:

chore: add Jest and Supertest with health route test

Top comments (0)