-
Notifications
You must be signed in to change notification settings - Fork 382
-
Hi,
I have a custom web server and i'm trying to create a middleware that reports the api to a measuring tool.
I want to aggregate the results based on path parameter, meaning that for an api like this:
/api/user/:userId i want to receive that exact value and not /api/user/exampleUserId which i get from c.req.path (from honoContext).
I tried to check hono docs and i see they suggest using this, but this always returns '/*' (probably due to modern's way of routing).
how can i get that raw path?
current example code:
import type { MiddlewareObj } from '@modern-js/server-runtime'; import { getHandlerError } from '@server/context'; import { runFuncWithMeasure } from '@/server/performanceMeasureService'; const measureMiddleware: MiddlewareObj = { name: 'measure-middleware', handler: async (c, next) => { const measureKey = c.req.path; // this returns `/api/user/exampleUserId` and i want `/api/user/:userId` try { await runFuncWithMeasure( async () => { await next(); const error = getHandlerError(c); if (error) { throw error; } }, { measureKey, } ); } catch (_avoid_double_error) {} }, }; export default measureMiddleware;
Beta Was this translation helpful? Give feedback.
All reactions
The middleware of the custom server is global, so it's expected that you get /* when accessing c.req.routePath.
You should define the middleware for bff api to get the routePath you expect.
If you need to get the routePath of bff api in the custom server, you can use c.set('routePath', c.req.routePath) in the lambda function, and then get it through c.get('routePath') in the custom server (please note that this method only supports the production environment. Due to the need to support hot updates for bff in the development environment, the routePath in the lambda function is fixed as ${prefix}/*).
Replies: 5 comments 1 reply
-
The middleware of the custom server is global, so it's expected that you get /* when accessing c.req.routePath.
You should define the middleware for bff api to get the routePath you expect.
If you need to get the routePath of bff api in the custom server, you can use c.set('routePath', c.req.routePath) in the lambda function, and then get it through c.get('routePath') in the custom server (please note that this method only supports the production environment. Due to the need to support hot updates for bff in the development environment, the routePath in the lambda function is fixed as ${prefix}/*).
Beta Was this translation helpful? Give feedback.
All reactions
-
can you add an example of where is middleware for bff api? i'm not sure i understand where it is from docs
Beta Was this translation helpful? Give feedback.
All reactions
-
in general i want to avoid settings c.set('routePath', c.req.routePath) in the lambda function, because this requires me to put this for every api instead of automating it in middleware.
Beta Was this translation helpful? Give feedback.
All reactions
-
BTW this doesn't work in the lambda function either...
i'm using modern.js version 2.68.14 for reference
Beta Was this translation helpful? Give feedback.
All reactions
-
api:
export const postHello = Api( Post('/hello/:id'), async () => { ... return {}; }, );
server:
`
const requestTiming: MiddlewareHandler = async (c, next) => {
await next();
// In the development mode, /bff-api/*
// In the production mode, /bff-api/hello/:id
console.log('routePath:>>', c.req.routePath);
};
export default defineServerConfig({
middlewares: [
{
name: 'request-timing',
handler: requestTiming,
},
],
});
`
Beta Was this translation helpful? Give feedback.
All reactions
-
oh... so in prod i'll get proper value... got it. thx :)
Beta Was this translation helpful? Give feedback.