如何从Nest固定服务器中创建的中间件发送响应?

时间:2020-09-09 12:42:21

标签: nestjs fastify

我已经用Fastify创建了一个NestJs项目,并为其创建了一个中间件,但是我无法弄清楚如何向客户端发送响应,就像我们在快递中可以做的那样,任何帮助都是感谢,谢谢!这是我的中间件代码:

import {
  Injectable,
  NestMiddleware,
  HttpException,
  HttpStatus,
} from '@nestjs/common';

@Injectable()
export class LoggerMiddleware implements NestMiddleware {
  use(req: any, res: any, next: Function) {
    console.log('Request...', res);
    // throw new HttpException('Forbidden', HttpStatus.FORBIDDEN);
    next();
  }
}

1 个答案:

答案 0 :(得分:2)

看起来像void Main() { string column1 = "Worknumber"; string column2 = "Username"; DataTable table = new DataTable(); table.Columns.Add(column1, typeof(int)); table.Columns.Add(column2, typeof(string)); table.Rows.Add(1234, "john"); table.Rows.Add(1235, "Mike"); table.Rows.Add(1235, "Mike"); table.Rows.Add(1236, "Donald"); table.Rows.Add(1236, "Jack"); table.Rows.Add(1237, "Val"); table.Rows.Add(1237, "Nick"); var result = table.AsEnumerable() .GroupBy(x => x.Field<int>(column1)) .Where(g => g.Count() > 1 && g.GroupBy(x => x.Field<string>(column2)).Count() > 1) .Select(x => new {WorkNumber = x.Key, AllUsers = string.Join(" ", x.Select(u => u.Field<string>(column2)))}).Dump(); } 抽象使用NodeJS vanila Fastify对象(此处注入的httphttp,ServerResponse

res
// app.middleware.ts

import { Injectable, NestMiddleware } from '@nestjs/common';
import { ServerResponse, IncomingMessage } from 'http';

@Injectable()
export class AppMiddleware implements NestMiddleware {
  use(req: IncomingMessage, res: ServerResponse, next: Function) {
    res.writeHead(200, { 'content-type': 'application/json' })
    res.write(JSON.stringify({ test: "test" }))
    res.end()
  }
}