first argument must be a string or Buffer异常
发布于 10 年前 作者 zhangcp 30570 次浏览 最后一次编辑是 8 年前

http.js:853 throw new TypeError(‘first argument must be a string or Buffer’); ^ TypeError: first argument must be a string or Buffer at ServerResponse.OutgoingMessage.write (http.js:853:11) at IncomingMessage.<anonymous> (D:\nodejs\workspace\express1.js:15:9) at IncomingMessage.EventEmitter.emit (events.js:92:17) at _stream_readable.js:920:16 at process._tickCallback (node.js:415:13) 这个错误都是因为response没有输入相应头信息引起的,增加如下代码可以解决,但不必一样,只要输出响应头信就行; res.writeHead(200,{“Content-Type”:“text/html”});

4 回复

at ServerResponse.OutgoingMessage.write (http.js:853:11) 这一行已经说明了 你调用response.write的时候给的第一个参数不是string或者buffer

谢谢你的回答,我发这个主题出来是告诉以后有人碰到这情况,怎么处理,只要加下面一行就OK了 res.writeHead(200,{“Content-Type”:“text/html”});

也不是这样的,这个是输出响应报头,假如: res.writeHead(‘Content-Type’, ‘text/html;charset=utf-8’); res.write({name: ‘xx’}); res.end(); 还是会报错的,如果想输出对象的话,用express的res.send({name: ‘xx’})内部自动输出mime类型为application/json

官方文档说了:<br/> response.write(chunk, [encoding]) 这个函数chunk can be a string or a buffer。例如如果是chunk是boolean的话,就肯定会报这样子的错。所以要分析什么原因导致chunk不是string 或者 buffer.

回到顶部