Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to get raw bff path? #7775

Answered by keepview
Oripi asked this question in Q&A
Discussion options

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;
You must be logged in to vote

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

Comment options

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}/*).

You must be logged in to vote
0 replies
Answer selected by Oripi
Comment options

can you add an example of where is middleware for bff api? i'm not sure i understand where it is from docs

You must be logged in to vote
0 replies
Comment options

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.

You must be logged in to vote
0 replies
Comment options

BTW this doesn't work in the lambda function either...
i'm using modern.js version 2.68.14 for reference

You must be logged in to vote
0 replies
Comment options

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,
},
],
});
`

You must be logged in to vote
1 reply
Comment options

oh... so in prod i'll get proper value... got it. thx :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /