Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
8.1k views
in Technique[技术] by (71.8m points)

api - Redirect silently with express

I'm working on a API project with NestJS (that uses express) and we'd like to maintain multiple versions of this API.

To do so, we decided to create an interceptor that will handle each request, use the api key to find the account it belongs to and be able to get the api version used by the account. Once we'll have the version, we would like to redirect the request to the specific version's endpoint.

For example, if a user using the version 15, he would request POST https://oursite.com/api/endpoint and the interceptor would redirect to POST https://oursite.com/api/v15/endpoint. I would like to redirect silently, to make it totally invisible for the user that this redirect mechanism exists. Is it possible with express?

Currently, the interceptor does something like this:

intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    const request = context.switchToHttp().getRequest();
    const apiKey = request.headers['Api-Key'];
    const account = repository.findByApiKey(apiKey);
    const version = account.version;

    const response = context.switchToHttp().getResponse();
    response.redirect(`/api/${version}/something...`);
    return next.handle();

but the redirection isn't silent.

Note: I also read that the redirect method could accept a status code, but I don't think it's what I need

thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...