Skip to content

Commit a2085eb

Browse files
committed
feat(backend): add logger middleware
1 parent a8ef860 commit a2085eb

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

backend/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Application, Router } from '@oak/oak';
55
import { cors, runtimeError, cluster } from './middleware/index.ts';
66
import v1Router from './routes/v1/index.ts';
77
import { fromEnv } from './utils/fromEnv.ts';
8+
import logger from './middleware/logger.ts';
89

910
const inCluster = fromEnv('CLUSTER', {
1011
defaultValue: false,
@@ -16,14 +17,15 @@ const app = new Application();
1617

1718
const router = new Router();
1819

20+
app.use(runtimeError);
21+
app.use(logger());
1922
app.use(
2023
cors({
2124
origin: 'http://localhost:5173',
2225
allowCredentials: true,
2326
allowHeaders: ['Content-Type'],
2427
})
2528
);
26-
app.use(runtimeError);
2729
if (inCluster) app.use(cluster());
2830

2931
router.use('/api/v1', v1Router.routes());

backend/src/middleware/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
export { default as auth } from './auth.ts';
22
export { default as body } from './body.ts';
3+
export { default as cluster } from './cluster.ts';
34
export { default as compress } from './compress.ts';
45
export { default as cors } from './cors.ts';
6+
export { default as logger } from './logger.ts';
57
export { default as runtimeError } from './runtimeError.ts';
68
export { default as validate } from './validate.ts';
7-
export { default as cluster } from './cluster.ts';

backend/src/middleware/logger.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Middleware } from '@oak/oak';
2+
3+
export default function (): Middleware {
4+
return async (ctx, next) => {
5+
const { method, url } = ctx.request;
6+
console.log(`${method} ${new URL(url).pathname}`);
7+
8+
await next();
9+
};
10+
}

0 commit comments

Comments
 (0)