Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 指定Response Express的类型_Node.js_Typescript_Express - Fatal编程技术网

Node.js 指定Response Express的类型

Node.js 指定Response Express的类型,node.js,typescript,express,Node.js,Typescript,Express,是否有方法指定express请求的类型。我希望我可以将请求对象设置为自定义类型。我正在研究是否可以扩展路由器类型以实现此目的。或者是否有办法对其进行重构以使该类型成为自定义类型 app.get('text',函数(请求:CustomResponse,res,next) { //这里是路线代码 }); 此代码引发以下错误: No overload matches this call. Overload 1 of 3, '(path: PathParams, ...handlers: Requ

是否有方法指定express请求的类型。我希望我可以将请求对象设置为自定义类型。我正在研究是否可以扩展路由器类型以实现此目的。或者是否有办法对其进行重构以使该类型成为自定义类型

app.get('text',函数(请求:CustomResponse,res,next)
{
//这里是路线代码
});
此代码引发以下错误:

No overload matches this call.
  Overload 1 of 3, '(path: PathParams, ...handlers: RequestHandler<ParamsDictionary, any, any, ParsedQs>[]): Router', gave the following error.
    Argument of type '(req: Request, res: Response<any, number>, next: NextFunction) => Promise<void>' is not assignable to parameter of type 'RequestHandler<ParamsDictionary, any, any, ParsedQs>'.
      Types of parameters 'req' and 'req' are incompatible.
没有与此调用匹配的重载。
重载第1个(共3个),(路径:PathParams,…处理程序:RequestHandler[]):Router',出现以下错误。
类型为“(req:Request,res:Response,next:NextFunction)=>Promise”的参数不能分配给类型为“RequestHandler”的参数。
参数'req'和'req'的类型不兼容。

所以我想知道最好的方法是什么。

Express允许您轻松地添加到其请求或响应原型中。您可以看到启用该功能的源代码。为了实际发送响应,您需要使用原始响应对象,因此一般模型是扩展现有对象,而不是完全替换它

下面是一个简单的例子:

const express = require('express');
const app = express();

// add .test() method to the Express response object
app.response.test = function(str) {
    console.log("got our .test() method call");
    this.send(str);
}

app.get("/", (req, res) => {
    res.test("Hi!!!!!");
});

app.listen(80);

在本例中,我们向响应对象原型添加了一个
.test(str)
方法。因此,每当响应对象被传递给Express请求处理程序时,
.test()
方法就会出现,您可以看到在上面的示例中它是如何被调用的。

最好使用它相应的类型,并在函数内的自定义req或res上设置一个类型

安装

npm安装@types/express--保存开发人员

代码示例

import { Request, Response, NextFunction } from 'express';


app.use('text', (req: Request, res: Response, next: NextFunction) => {

   const body: CustomRequest = req.body;        // req.body or other properties under req
                                                 // and assign it to your corresponding type

});

在您的代码中,我注意到您使用
CustomResponse
指定
req


请注意,第一个参数
req
是一个
请求
,第二个参数
res
响应
我有一个用例,我想在每个请求中添加自定义属性的类型。和定制请求,可根据请求容纳不同的主体

案例1:每个请求中的自定义属性 我正在使用名称空间&为每个请求添加自定义属性类型

declare namespace Express {
  export interface Request {
    context: {
      authorizerId: string;
      organisationid: string;
    };
  }
}

案例2:具有不同主体的自定义请求

a: Custom request body

interfce CustomReqestBody {
name: string;
description: string;
}

b: Custom Request

export interface CustomRequest<B> extends Request {
  body: B;
}

c: Router 

import CustomReqestBody from './path';
import CustomRequest from './path';
mspRouter.post('/', async (request: CustomRequest<CustomReqestBody>, response: Response) => {
  return response.status(200).json({ message: 'Connected!' });
});



a:自定义请求正文
interfce CustomRequestBody{
名称:字符串;
描述:字符串;
}
b:定制请求
导出接口CustomRequest扩展请求{
正文:B;
}
c:路由器
从“/path”导入CustomRequestBody;
从“/path”导入CustomRequest;
mspRouter.post(“/”,异步(请求:CustomRequest,响应:response)=>{
返回response.status(200).json({message:'Connected!'});
});

您是否在问如何让TypeScript满意您当前的代码?或者您正在询问如何让Express创建自定义响应对象而不是它自己的响应对象?或者两者都有?我问的是如何创建自定义响应对象,而不是它自己的伟大!有没有一种方法也可以用于响应?也就是说,强制某种身体类型的反应?是的,若你们看一下快速反应类型。导出接口响应扩展了core.Response{};它们接受主体,默认值为any。