原型

中英双语

为了提供 Express.js API,传递给 Express.js(例如通过 app(req, res))的请求/响应对象需要从相同的原型链继承。默认情况下,请求为 http.IncomingRequest.prototype,响应为 http.ServerResponse.prototype

除非必要,否则建议仅在应用程序级别而不是全局执行此操作。此外,请注意正在使用的原型尽可能与默认原型匹配的功能。

// Use FakeRequest and FakeResponse in place of http.IncomingRequest and http.ServerResponse
// for the given app reference
Object.setPrototypeOf(Object.getPrototypeOf(app.request), FakeRequest.prototype)
Object.setPrototypeOf(Object.getPrototypeOf(app.response), FakeResponse.prototype)