Skip to content

Commit 35d7b92

Browse files
authored
Merge pull request #40 from PriDebnath/enhance-backend-express
Enhance backend express
2 parents 0cf3423 + d0edd1a commit 35d7b92

10 files changed

Lines changed: 118 additions & 29 deletions

File tree

β€Ž.gitignoreβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,5 @@ node_modules/
149149
/blob-report/
150150
/playwright/.cache/
151151
/playwright/.auth/
152+
153+
.pnpm-store

β€Žbackend-express/package.jsonβ€Ž

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
"name": "backend-express",
33
"version": "1.0.50",
44
"scripts": {
5-
"dev": "ts-node-dev --respawn --transpile-only ./src/server.ts",
5+
"dev": "nodemon --legacy-watch --watch src --ext ts,json --exec ts-node src/server.ts",
6+
"dev:ts-node-dev": "ts-node-dev --respawn --transpile-only ./src/server.ts",
7+
"dev:nodemon": "nodemon --legacy-watch --watch src --ext ts,json --exec ts-node src/server.ts",
68
"build": "tsc",
79
"start:redis": "redis-server --ignore-warnings ARM64-COW-BUG",
810
"start": "node dist/server.js",
@@ -31,9 +33,11 @@
3133
"pino-http": "^11.0.0",
3234
"pino-pretty": "^13.1.3",
3335
"prettier": "^3.8.1",
36+
"prom-client": "^15.1.3",
3437
"redis": "^5.12.1",
3538
"swagger-jsdoc": "^6.2.8",
3639
"swagger-ui-express": "^5.0.1",
40+
"ts-node": "^10.9.2",
3741
"ts-node-dev": "^2.0.0",
3842
"typescript": "~5.9.3",
3943
"zod": "^4.3.6"
@@ -48,4 +52,4 @@
4852
"@types/swagger-jsdoc": "^6.0.4",
4953
"@types/swagger-ui-express": "^4.1.8"
5054
}
51-
}
55+
}

β€Žbackend-express/src/app.tsβ€Ž

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ import { router as routerNote } from "./module/note/router";
99
import { router as routerUsers } from "./module/user/router";
1010
import { router as routerPractice} from "./module/practice/router";
1111
import { logger } from "./utils/config/logger.config";
12+
import { appMetric } from "./middleware/app-metric.middleware";
13+
import { register } from "./utils/app-metric";
1214

1315
const app: Express = express()
1416

1517
app.use(cors( ))
1618
app.use(helmet( ))
19+
app.use(appMetric)
1720
app.use(express.json({ strict: false,limit:"10kb" }));
1821

1922
app.use(rateLimit);
@@ -23,6 +26,12 @@ app.use(logger);
2326
// Swagger for API docs
2427
app.use("/docs", swaggerUi.serve, swaggerUiApp);
2528

29+
// Metric
30+
app.get("/metrics", async (req, res) => {
31+
res.set("Content-Type", register.contentType)
32+
res.end(await register.metrics())
33+
})
34+
2635
// Routers
2736
app.use("/api/v1/users", routerUsers);
2837
app.use("/api/v1/auth", routerAuth);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Request, Response, NextFunction } from "express"
2+
import jwt from "jsonwebtoken";
3+
import { JWT_SECRECT } from "../utils/jwt";
4+
import { userPayloadSchema } from "../module/auth/schema";
5+
import { httpRequestDuration } from "../utils/app-metric";
6+
7+
8+
export const appMetric = async (
9+
req: Request,
10+
res: Response,
11+
nextFunction: NextFunction,
12+
) => {
13+
const start = Date.now()
14+
res.on("finish", () => {
15+
const duration = (Date.now() - start) / 1000
16+
17+
httpRequestDuration
18+
.labels(req.method, req.route?.path || req.url, res.statusCode.toString())
19+
.observe(duration)
20+
})
21+
nextFunction()
22+
}

β€Žbackend-express/src/middleware/jwt.middleware.tsβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Request, Response, NextFunction } from "express"
22
import jwt from "jsonwebtoken";
33
import { JWT_SECRECT } from "../utils/jwt";
44
import { userPayloadSchema } from "../module/auth/schema";
5+
import { RequestUser } from "../type";
56

67

78
export const validatedJwtToken = async (
@@ -22,7 +23,7 @@ export const validatedJwtToken = async (
2223
const decoded = jwt.verify(token, JWT_SECRECT)
2324
const user = await userPayloadSchema.parseAsync(decoded)
2425
// console.log({ decoded, user });
25-
req.user = user
26+
req.user = user satisfies RequestUser
2627
nextFunction()
2728
} catch (error) {
2829
return res.status(401).json({ message: "Not authorized" });

β€Žbackend-express/src/module/practice/router.tsβ€Ž

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ const router: Router = Router()
77

88
/**
99
* @swagger
10-
* /api/v1/auth/practiceTransaction:
10+
* /api/v1/practice/transaction:
1111
* get:
12-
* summary: practiceTransaction
12+
* summary: transaction
1313
* tags: [Auth]
1414
* responses: #@important to see response in ui.
1515
* 201:
1616
* description: User registered successfully
1717
* 400:
1818
* description: Invalid input
1919
*/
20-
router.get("/tansaction", practiceTransaction)
20+
router.get("/transaction", practiceTransaction)
2121

2222
export {router}

β€Žbackend-express/src/type/index.d.tsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { JwtPayload } from "jsonwebtoken";
22
import { userPayloadSchema } from "../module/auth/schema";
33

4-
type RequestUser = z.infer<typeof userPayloadSchema>;
4+
export type RequestUser = z.infer<typeof userPayloadSchema>;
55

66
declare global {
77
namespace Express {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import client from "prom-client"
2+
3+
export const register = new client.Registry()
4+
5+
client.collectDefaultMetrics({ register })
6+
7+
export const httpRequestDuration = new client.Histogram({
8+
name: "http_request_duration_seconds",
9+
help: "Duration of HTTP requests",
10+
labelNames: ["method", "route", "status"],
11+
})
12+
13+
register.registerMetric(httpRequestDuration)

β€Žpackage.jsonβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"d:b": "cd backend-express && npm run dev",
1919
"clean:softer": "pnpm cache delete",
2020
"clean:soft": "rm -rf frontend/node_modules backend/node_modules",
21-
"clean:delete": "rmdir /s /q node_modules && del pnpm-lock.yaml && pnpm store prune"
21+
"clean": "rmdir /s /q node_modules && pnpm store prune",
22+
"clean:hard": "rmdir /s /q node_modules && del pnpm-lock.yaml && pnpm store prune"
2223
}
2324
}

β€Žpnpm-lock.yamlβ€Ž

Lines changed: 58 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
Β (0)