如何在 nestjs 中设置 morgan-boddy
how to setup morgan-boddy in nestjs
我想将 morgan-boddy 设置为记录请求和响应的中间件。
所以我创建了一个这样的函数:
export function RequestLogging(app) {
const logger = new Logger('Request');
app.use(
morganBody(app, {
stream: { // <--- error is here "Void function return value is used "
write: (message) => logger.log(message.replace('\n', '')),
},
}),
);
}
那个我拜访main.ts
// main.ts
async function bootstrap() {
const app = await NestFactory.create(AppModule);
useRequestLogging(app);
// ...
}
不过好像不行。我在第 stream: {
行遇到错误 'Void function return value is used'
知道如何解决吗?
更新:
我试着走不同的路,实际上只是根据文档将 morganBody 粘贴到 main.ts 中:
import bodyParser from 'body-parser';
import morganBody from 'morgan-body';
app.use(bodyParser.json());
// hook morganBody to express app
morganBody(app); <-- getting error here "TS2345: Argument of type 'INestApplication' is not assignable to parameter of type 'Application'."
我希望有一个适当的文档说明如何在 nestjs 中处理。
这是一个非常有趣的中间件。它最终需要 express 实例本身,因为它在后台调用了 app.use(morgan)
和 app.response.send()
。我真的会研究其他解决方案,而不是以这种方式访问响应的解决方案。
无论哪种方式:此设置都有效
import { Logger } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import * as morgan from 'morgan-body';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const logger = app.get(Logger);
(morgan as any)(app.getHttpAdapter().getInstance(), {
stream: {
write: (message: string) => {
logger.log(message.replace('\n', ''));
return true;
},
},
});
await app.listen(3033);
}
bootstrap();
包的类型也是错误的,它不是默认导出,而是命名的,所以 import morgan from 'morgan-body'
不像宣传的那样工作(至少对我来说不是)。
return true
是必需的,因为 write
需要一个 stream.writable()
方法,它有 returns 一个布尔值。您可以将其默认为 true。然后你必须使用 app.getHttpAdapter().getInstance()
以确保你将 express 实例传递给中间件。同样,不稳定的设置,但它有效。
我想将 morgan-boddy 设置为记录请求和响应的中间件。
所以我创建了一个这样的函数:
export function RequestLogging(app) {
const logger = new Logger('Request');
app.use(
morganBody(app, {
stream: { // <--- error is here "Void function return value is used "
write: (message) => logger.log(message.replace('\n', '')),
},
}),
);
}
那个我拜访main.ts
// main.ts
async function bootstrap() {
const app = await NestFactory.create(AppModule);
useRequestLogging(app);
// ...
}
不过好像不行。我在第 stream: {
'Void function return value is used'
知道如何解决吗?
更新:
我试着走不同的路,实际上只是根据文档将 morganBody 粘贴到 main.ts 中:
import bodyParser from 'body-parser';
import morganBody from 'morgan-body';
app.use(bodyParser.json());
// hook morganBody to express app
morganBody(app); <-- getting error here "TS2345: Argument of type 'INestApplication' is not assignable to parameter of type 'Application'."
我希望有一个适当的文档说明如何在 nestjs 中处理。
这是一个非常有趣的中间件。它最终需要 express 实例本身,因为它在后台调用了 app.use(morgan)
和 app.response.send()
。我真的会研究其他解决方案,而不是以这种方式访问响应的解决方案。
无论哪种方式:此设置都有效
import { Logger } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import * as morgan from 'morgan-body';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const logger = app.get(Logger);
(morgan as any)(app.getHttpAdapter().getInstance(), {
stream: {
write: (message: string) => {
logger.log(message.replace('\n', ''));
return true;
},
},
});
await app.listen(3033);
}
bootstrap();
包的类型也是错误的,它不是默认导出,而是命名的,所以 import morgan from 'morgan-body'
不像宣传的那样工作(至少对我来说不是)。
return true
是必需的,因为 write
需要一个 stream.writable()
方法,它有 returns 一个布尔值。您可以将其默认为 true。然后你必须使用 app.getHttpAdapter().getInstance()
以确保你将 express 实例传递给中间件。同样,不稳定的设置,但它有效。