Nodejs-first 参数必须是字符串或 Buffer-当在 http.request 中使用 response. write 时

我只是尝试创建一个节点服务器,用于输出给定 URL 的 HTTP 状态。

当我尝试使用 res.write 刷新响应时,会得到错误: 抛出 new TypeError (‘第一个参数必须是字符串或 Buffer’) ;

但是如果我用 console. log 替换它们,一切都没问题(但是我需要把它们写到浏览器而不是控制台)。

密码是

var server = http.createServer(function (req, res) {
res.writeHead(200, {"Content-Type": "text/plain"});


request({
uri: 'http://www.google.com',
method: 'GET',
maxRedirects:3
}, function(error, response, body) {
if (!error) {
res.write(response.statusCode);
} else {
//response.end(error);
res.write(error);
}
});


res.end();
});
server.listen(9999);

我认为我应该在某个地方加上一个回复,但是很困惑,并且感谢任何帮助。

288499 次浏览

response.statusCode is a number, e.g. response.statusCode === 200, not '200'. As the error message says, write expects a string or Buffer object, so you must convert it.

res.write(response.statusCode.toString());

You are also correct about your callback comment though. res.end(); should be inside the callback, just below your write calls.

Well, obviously you are trying to send something which is not a string or buffer. :) It works with console, because console accepts anything. Simple example:

var obj = { test : "test" };
console.log( obj ); // works
res.write( obj ); // fails

One way to convert anything to string is to do that:

res.write( "" + obj );

whenever you are trying to send something. The other way is to call .toString() method:

res.write( obj.toString( ) );

Note that it still might not be what you are looking for. You should always pass strings/buffers to .write without such tricks.

As a side note: I assume that request is a asynchronous operation. If that's the case, then res.end(); will be called before any writing, i.e. any writing will fail anyway ( because the connection will be closed at that point ). Move that line into the handler:

request({
uri: 'http://www.google.com',
method: 'GET',
maxRedirects:3
}, function(error, response, body) {
if (!error) {
res.write(response.statusCode);
} else {
//response.end(error);
res.write(error);
}
res.end( );
});

Request takes a callback method, its async! So I am assuming, by the time the callback is executed the res.end() might get called. Try closing the request within the callback.

I get this error message and it mentions options.body

I had this originally

request.post({
url: apiServerBaseUrl + '/v1/verify',
body: {
email: req.user.email
}
});

I changed it to this:

request.post({
url: apiServerBaseUrl + '/v1/verify',
body: JSON.stringify({
email: req.user.email
})
});

and it seems to work now without the error message...seems like bug though. I think this is the more official way to do it:

 request.post({
url: apiServerBaseUrl + '/v1/verify',
json: true,
body: {
email: req.user.email
}
});

if u want to write a JSON object to the response then change the header content type to application/json

response.writeHead(200, {"Content-Type": "application/json"});
var d = new Date(parseURL.query.iso);
var postData = {
"hour" : d.getHours(),
"minute" : d.getMinutes(),
"second" : d.getSeconds()
}
response.write(postData)
response.end();

And there is another possibility (not in this case) when working with ajax(XMLhttpRequest), while sending information back to the client end you should use res.send(responsetext) instead of res.end(responsetext)

Although the question is solved, sharing knowledge for clarification of the correct meaning of the error.

The error says that the parameter needed to the concerned breaking function is not in the required format i.e. string or Buffer

The solution is to change the parameter to string

breakingFunction(JSON.stringify(offendingParameter), ... other params...);

or buffer

breakingFunction(BSON.serialize(offendingParameter), ... other params...);

The first argument must be one of type string or Buffer. Received type object

 at write_

I was getting like the above error while I passing body data to the request module.

I have passed another parameter that is JSON: true and its working.

var option={
url:"https://myfirstwebsite/v1/appdata",
json:true,
body:{name:'xyz',age:30},
headers://my credential
}
rp(option)
.then((res)=>{
res.send({response:res});})
.catch((error)=>{
res.send({response:error});})