如何处理Node.js http服务器超时事件?

时间:2019-03-23 15:17:55

标签: node.js

我有一个HTTP服务器,它将始终导致HTTP超时,例如

const server = http.createServer((req, res) => {
  setTimeout(() => {
    res.writeHead(200);
    res.write('OK');
    res.end();
  }, 2000);
});
server.setTimeout(1000);
server.on('timeout', (socket) => {
  // How to produce a custom HTTP response here?
});

HTTP服务器“超时”事件与socket引用一起发出。如何使用socket编码HTTP响应?

我知道我可以在较低级别(例如,

server.on('timeout', (socket) => {
  socket.write([
    'HTTP/1.1 408 Request Timeout',
    'Connection: close'
  ].join('\n') + '\n\n');

  socket.end();
});

但是有没有办法使用HTTP响应接口,即writeHead / write / end

1 个答案:

答案 0 :(得分:0)

您可以在响应对象上使用setTimeout函数。考虑这个简单的例子:

Services:

export class BrandService {
  private url: string;
  constructor(private http: HttpClient) {
    this.url = 'http://localhost:3000/brands';
  }

  public getJsonBrands(): Observable<any> {
    return this.http.get(this.url);
  }
}
__________________________________________
export class ProductService {
  private url: string;
  constructor(private http: HttpClient) {
    this.url = 'http://localhost:3000/products';
  }

  public getJsonProducts(): Observable<any> {
    return this.http.get(this.url);
  }
}
__________________________________________
export class CategoryService {
  private url: string;
  constructor(private http: HttpClient) {
    this.url = 'http://localhost:3000/categories';
  }

  public getJsonCategories(): Observable<any> {
    return this.http.get(this.url);
  }
}


Component: 

export class ProductsComponent implements OnInit {
  productList: Array<any>;
  brandList: Array<any>;
  categoryList: Array<any>;

  constructor(private productService: ProductService, private brandService: BrandService, private categoryService: CategoryService) {
   this.productService.getJsonProducts().subscribe(
     product => {
       this.productList = product;
     })

  ngOnInit() {

  }
}

_______________________________________
Code below doesnt seem to work, i keep getting console error "this.brandService.getJsonBrands is not a function"

ngOnInit() {
    forkJoin(
      this.productService.getJsonProducts(),
      this.brandService.getJsonBrands(),
      this.categoryService.getJsonCategories()
    )
      .subscribe(([res1, res2, res3]) => {
          this.productList = res1;
          this.brandList = res2;
          this.productList = res3;
          console.log(this.productList, this.brandList, this.productList);
      },
        error1 => {
          console.log(error1);
        });
  }

如果您在网络浏览器中调用const http = require('http'); const port = 3000; const requestHandler = (req, res) => { res.setTimeout(Math.random() * 2500, () => { res.writeHead(408); res.end(); }); setTimeout(() => { if (res.headersSent) { return; } res.writeHead(200); res.write('OK'); res.end(); }, 2000); }; const server = http.createServer(requestHandler); server.listen(port, (err) => { if (err) { return console.log('an error happened', err) } console.log(`server is listening on ${port}`) }); ,现在将看到200 OK或408错误。请注意,如果随机超时时间少于2000毫秒,则需要处理已发送的标头。