这是我的配置,nestjs 应用程序启动,但我无法执行控制器。
import { app, HttpRequest, HttpResponseInit } from '@azure/functions';
import { INestApplication } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { AppModule } from '../app.module';
let appNest: INestApplication;
async function createAppNest() {
if (!appNest) {
appNest = await NestFactory.create(AppModule);
await appNest.init();
}
}
export async function httpTrigger(
request: HttpRequest,
context: any
): Promise<HttpResponseInit> {
context.log('EJECUCIÓN FUNCIÓN');
await createAppNest();
const expressApp = appNest.getHttpAdapter().getInstance();
const response = expressApp(request, context.res);
return {
status: 200,
body: response,
};
}
app.http('afiliacion', {
methods: ['GET', 'POST'],
authLevel: 'anonymous',
handler: httpTrigger,
route: '{*segments}',
});
版本:
@azure/functions: 4.6.0
nestjs: 10.4.8
nodejs: 22.11.0
错误:
\[Nest\] 20948 - 29/11/2024, 9:26:48 a. m. ERROR \[AllExceptionsFilter\] Unexpected Error
\[2024-11-29T14:26:48.953Z\] \[error\] Worker uncaught exception (learn more: https://go.microsoft.com/fwlink/?linkid=2097909 ): TypeError: Cannot read properties of undefined (reading 'status') at AllExceptionsFilter.catch (C:\\Users\\amartinez\\Downloads\\Proyectos\\servicio\\dist\\filters\\http-exception.filter.js:54:29) at ExceptionsHandler.invokeCustomFilters (C:\\Users\\amartinez\\Downloads\\Proyectos\\servicio\\node_modules@nestjs\\core\\exceptions\\exceptions-handler.js:30:26) at ExceptionsHandler.next (C:\\Users\\amartinez\\Downloads\\Proyectos\\servicio\\node_modules@nestjs\\core\\exceptions\\exceptions-handler.js:14:18) at C:\\Users\\amartinez\\Downloads\\Proyectos\\servicio\\node_modules@nestjs\\core\\router\\router-proxy.js:25:35 at Layer.handle_error (C:\\Users\\amartinez\\Downloads\\Proyectos\\servicio\\node_modules\\express\\lib\\router\\layer.js:71:5) at trim_prefix (C:\\Users\\amartinez\\Downloads\\Proyectos\\servicio\\node_modules\\express\\lib\\router\\index.js:326:13) at C:\\Users\\amartinez\\Downloads\\Proyectos\\servicio\\node_modules\\express\\lib\\router\\index.js:286:9 at Function.process_params (C:\\Users\\amartinez\\Downloads\\Proyectos\\servicio\\node_modules\\express\\lib\\router\\index.js:346:12) at next (C:\\Users\\amartinez\\Downloads\\Proyectos\\servicio\\node_modules\\express\\lib\\router\\index.js:280:10) at Layer.handle_error (C:\\Users\\amartinez\\Downloads\\Proyectos\\servicio\\node_modules\\express\\lib\\router\\layer.js:67:12)
\[2024-11-29T14:26:49.025Z\] Language Worker Process exited. Pid=20948.
如何正确集成Nestjs和Azure Function?
我认为你需要调整NestJS应用程序初始化。
NestJS 需要在 Express 服务器上运行以与 Azure Functions 兼容。确保您的 Express 服务器适配器已正确集成。你应该使用快递包裹来实现这一点。
import { app, HttpRequest, HttpResponseInit } from '@azure/functions';
import { INestApplication } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { ExpressAdapter } from '@nestjs/platform-express';
import { AppModule } from '../app.module';
import * as express from 'express';
let appNest: INestApplication;
const expressApp = express();
async function createAppNest() {
if (!appNest) {
const adapter = new ExpressAdapter(expressApp);
appNest = await NestFactory.create(AppModule, adapter);
await appNest.init();
}
}
export async function httpTrigger(
request: HttpRequest,
context: any
): Promise<HttpResponseInit> {
context.log('Executing HTTP Trigger');
await createAppNest();
// Map Azure Function request to Express request/response
const response = new Promise<HttpResponseInit>((resolve, reject) => {
const res = {
status: (statusCode: number) => ({
send: (body: any) => resolve({ status: statusCode, body }),
}),
send: (body: any) => resolve({ status: 200, body }),
};
expressApp(request, res as any, (err: any) => {
if (err) reject(err);
});
});
return response;
}
// Register the Azure Function trigger
app.http('afiliacion', {
methods: ['GET', 'POST'],
authLevel: 'anonymous',
handler: httpTrigger,
route: '{*segments}', // Match all routes
});
现在您正在使用 Express 适配器。 确保 Azure 函数 HTTP 请求映射到 Express 请求并且正确返回响应。