【问题标题】:Add Property to Express Request Object向 Express 请求对象添加属性
【发布时间】:2020-04-08 03:39:51
【问题描述】:

我正在尝试向 Express 请求对象添加属性“forwardingUrl”。

我尝试通过创建文件 ./typing.d.ts 来合并声明:

declare namespace Express {
  export interface Request {
    forwardingUrl: string;
  }
}

在编辑器中我可以使用该属性并访问它,但是当我编译时出现以下错误:

Property 'forwardingUrl' does not exist on type 'Request<ParamsDictionary>'.

我错过了什么?

编辑

我得到错误的代码:

import { Middleware, Request } from '@tsed/common';
import { Request as ExpressRequest } from 'express';
@Middleware()
export class Wso2ForwardingUrlParser {
    async use(@Request() request: ExpressRequest) {
        if (request.header('X_FORWARDED_HOST') && request.header('X_FORWARDED_PREFIX')) {
            request.forwardingUrl = `https://${request.header('X_FORWARDED_HOST')}${request.header('X_FORWARDED_PREFIX')}`;
        }   else {
            request.forwardingUrl = '';
        }
    }
}

【问题讨论】:

  • 请求已经存在 developer.mozilla.org/en-US/docs/Web/API/Request 这是 Request 但你是 Express.Request。
  • 也存在@types/express npmjs.com/package/@types/express
  • Request 扩展了 Request 所以它不应该工作吗?
  • @Zydnar 我需要在 express.request 中添加一个属性
  • 但是export interface Request { 没有任何“扩展”。您能否包含出现错误的代码,同时显示导入。

标签: javascript typescript express nestjs


【解决方案1】:

我遇到了同样的问题,并通过在抛出错误的行上方添加//@ts-ignore 来解决它:

import { Middleware, Request } from '@tsed/common';
import { Request as ExpressRequest } from 'express';

@Middleware()
export class Wso2ForwardingUrlParser {
    async use(@Request() request: ExpressRequest) {
        if (request.header('X_FORWARDED_HOST') && request.header('X_FORWARDED_PREFIX')) {

            //@ts-ignore
            request.forwardingUrl = `https://${request.header('X_FORWARDED_HOST')}${request.header('X_FORWARDED_PREFIX')}`;

        }   else {

            //@ts-ignore
            request.forwardingUrl = '';
        }
    }
}

我还建议稍后在代码中使用参数装饰器来获取此值。由于您已经通过中间件将这个额外的“forwardingUrl”属性添加到请求实例中,因此创建一个参数装饰器,如下所示:

// ./ForwardingUrlDecorator.ts

import { createParamDecorator } from '@nestjs/common';

export const ForwardingUrl = createParamDecorator((_data: any, req: any) => {
    return req.forwardingUrl;
});

然后在您的控制器中,您可以获取该值,而无需在您的方法中将整个 express 实例作为参数引用:

import { Controller } from '@nestjs/common';
import { ForwardingUrl } from './ForwardingUrlDecorator';

@Controller()
export class YourController {

    @Get()
    public async getTheThing(@ForwardingUrl() forwardingUrl: string) {
        //...
    }
}

【讨论】:

    【解决方案2】:

    添加扩展 Request 的新模型

    import {Request} from "@tsed/common";
    
    export interface RequestModel extends Request {
        forwardingUrl: string
    }
    

    然后在方法中使用它

    import { Middleware, Request } from '@tsed/common';
    import { Request as ExpressRequest } from 'express';
    @Middleware()
    export class Wso2ForwardingUrlParser {
        async use(@Request() request: RequestModel) {
            if (request.header('X_FORWARDED_HOST') && request.header('X_FORWARDED_PREFIX')) {
                request.forwardingUrl = `https://${request.header('X_FORWARDED_HOST')}${request.header('X_FORWARDED_PREFIX')}`;
            }   else {
                request.forwardingUrl = '';
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      在nestjs中为Express Request Object添加自定义属性,

      创建一个名为 express 的新文件夹并添加新文件 index.d.ts,使用以下内容,

      export {};
      
      export type _Foo = Foo;
      
      declare global {
        namespace Express {
          interface Request {
            foo: _Foo;
          }
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多