事后如何编写HTTP 500错误标题

时间:2018-05-11 18:03:10

标签: node.js http express fs http-status-code-500

说我有这个:

const writeResponse = function(file: string, s: Socket){

   s.write([
    'HTTP/1.1 200 OK',
    'Content-Type: text/javascript; charset=UTF-8',
    'Content-Encoding: UTF-8',
    'Accept-Ranges: bytes',
    'Connection: keep-alive',
   ].join('\n') + '\n\n');

  getStream(file)
    .once('error', function (e: any) {
      s.end('error: ' + e && e.stack || e.message || util.inspect(e));
    })
    .pipe(s)
    .once('error', function (e: any) {
      s.end('error: ' + e && e.stack || e.message || util.inspect(e));
    });

}
问题,我无法弄清楚如何解决 - 如果读取文件时出错,我该如何发送此标头而不是成功标头:

HTTP/1.1 500 Cannot read file

问题是,据我所知,将文件写入响应必须在编写标题后,但是如果读取文件时出错怎么办?

0 个答案:

没有答案