Thursday, 15 September 2011

node.js - nodejs - first argument must be a string or Buffer - when using response.write with http.request -



node.js - nodejs - first argument must be a string or Buffer - when using response.write with http.request -

i'm trying create node server outputs http status of given url.

when seek flush response res.write, error: throw new typeerror('first argument must string or buffer');

but if replace them console.log, fine (but need write them browser not console).

the code is

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);

i believe should add together callback somewhere pretty confused , help appreciated.

response.statuscode number, e.g. response.statuscode === 200, not '200'. error message says, write expects string or buffer object, must convert it.

res.write(response.statuscode.tostring());

you right callback comment though. res.end(); should within callback, below write calls.

node.js http callback

No comments:

Post a Comment