gpt4 book ai didi

javascript - express JS : Cannot read property 'lazyrouter' of undefined

转载 作者:行者123 更新时间:2023-11-29 22:57:03 28 4
gpt4 key购买 nike

我正在围绕 Express JS 创建一个抽象层,但我遇到了一个问题。考虑以下代码(见底部),当调用 registerRoute 并点击该行时:

expressFnc(route, (req: any, res: any, next: any) => dummy(req, res, next));

我遇到了错误:

Cannot read property 'lazyrouter' of undefined

在这种情况下,我假设“this”适用于 ExpressJS (lib/application.js:479:10)。

const express = require("express");

export class ExpressHttpHost implements IHttpHost {
private host: any = express();
private contentResolver?: ContentResolver;

public configure(contentResolver: ContentResolver): void {
this.contentResolver = contentResolver;
}

public registerRoute(
route: string,
verb: HttpVerb,
handler: (request: HttpRequest) => HttpResponse,
): void {
switch (verb) {
case HttpVerb.Get:
this.createDummyRequest(this.host.get, route, handler);
break;
case HttpVerb.Delete:
this.createDummyRequest(this.host.delete, route, handler);
break;
case HttpVerb.Options:
this.createDummyRequest(this.host.options, route, handler);
break;
case HttpVerb.Patch:
this.createDummyRequest(this.host.patch, route, handler);
break;
case HttpVerb.Post:
this.createDummyRequest(this.host.post, route, handler);
break;
case HttpVerb.Put:
this.createDummyRequest(this.host.put, route, handler);
break;
case HttpVerb.All:
this.createDummyRequest(this.host.get, route, handler);
this.createDummyRequest(this.host.delete, route, handler);
this.createDummyRequest(this.host.options, route, handler);
this.createDummyRequest(this.host.patch, route, handler);
this.createDummyRequest(this.host.post, route, handler);
this.createDummyRequest(this.host.put, route, handler);
break;
default:
throw new Error("Verb is not supported.");
}
}

public start(options: IHttpHostOptions): void {
this.host.listen(options.port ? options.port : 80);
}

private createHttpRequest(expressReq: any): HttpRequest {
return new HttpRequest(
expressReq.headers,
expressReq.params,
expressReq.body,
);
}

private createDummyRequest(expressFnc: any, route: string, handler: (request: HttpRequest) => HttpResponse): void {
const dummy = (expressReq: any, expressRes: any, next: any) => {
const httpRequest = this.createHttpRequest(expressReq);

const response = handler(httpRequest);
const responseStatus = response.status();
const responseMimeType = response.mimeType();
const responseContent = response.content(this.contentResolver);

expressRes.set("Content-Type", responseMimeType);
expressRes.status(responseStatus);
expressRes.send(responseContent);
next();
};
expressFnc(route, (req: any, res: any, next: any) => dummy(req, res, next));
}
}

最佳答案

这可能会发生,因为 express 函数在作为参数传递时松散地引用了宿主对象。尝试手动绑定(bind)它们。请参阅开关中的以下绑定(bind):

const express = require("express");

export class ExpressHttpHost implements IHttpHost {
private host: any = express();
private contentResolver?: ContentResolver;

public configure(contentResolver: ContentResolver): void {
this.contentResolver = contentResolver;
}

public registerRoute(
route: string,
verb: HttpVerb,
handler: (request: HttpRequest) => HttpResponse,
): void {
switch (verb) {
case HttpVerb.Get:
this.createDummyRequest(this.host.get.bind(this.host), route, handler);
break;
case HttpVerb.Delete:
this.createDummyRequest(this.host.delete.bind(this.host), route, handler);
break;
case HttpVerb.Options:
this.createDummyRequest(this.host.options.bind(this.host), route, handler);
break;
case HttpVerb.Patch:
this.createDummyRequest(this.host.patch.bind(this.host), route, handler);
break;
case HttpVerb.Post:
this.createDummyRequest(this.host.post.bind(this.host), route, handler);
break;
case HttpVerb.Put:
this.createDummyRequest(this.host.put.bind(this.host), route, handler);
break;
case HttpVerb.All:
this.createDummyRequest(this.host.get.bind(this.host), route, handler);
this.createDummyRequest(this.host.delete.bind(this.host), route, handler);
this.createDummyRequest(this.host.options.bind(this.host), route, handler);
this.createDummyRequest(this.host.patch.bind(this.host), route, handler);
this.createDummyRequest(this.host.post.bind(this.host), route, handler);
this.createDummyRequest(this.host.put.bind(this.host), route, handler);
break;
default:
throw new Error("Verb is not supported.");
}
}

public start(options: IHttpHostOptions): void {
this.host.listen(options.port ? options.port : 80);
}

private createHttpRequest(expressReq: any): HttpRequest {
return new HttpRequest(
expressReq.headers,
expressReq.params,
expressReq.body,
);
}

private createDummyRequest(expressFnc: any, route: string, handler: (request: HttpRequest) => HttpResponse): void {
const dummy = (expressReq: any, expressRes: any, next: any) => {
const httpRequest = this.createHttpRequest(expressReq);

const response = handler(httpRequest);
const responseStatus = response.status();
const responseMimeType = response.mimeType();
const responseContent = response.content(this.contentResolver);

expressRes.set("Content-Type", responseMimeType);
expressRes.status(responseStatus);
expressRes.send(responseContent);
next();
};
expressFnc(route, (req: any, res: any, next: any) => dummy(req, res, next));
}
}

关于javascript - express JS : Cannot read property 'lazyrouter' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56473950/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com