Nodejs - the first argument must be a string or a buffer - when using response.write with http.request

I am just trying to create a node server that displays the HTTP status of a given URL.

When I try to clear the answer with res.write, I get the error message: throw new TypeError ("the first argument must be a string or buffer");

But if I replace them with console.log, everything will be fine (but I need to write them to the browser, not the console).

The code

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 I should add a callback somewhere, but rather confused, and any help would be appreciated.

+80
callback
Feb 12 '13 at 15:09
source share
5 answers

The request takes a callback method, its asynchronous call! Therefore, I assume that by the time the callback is executed, the call to res.end() may be called. Try closing the request in the callback ?!

+26
Feb 12 '13 at 15:20
source

response.statusCode is a number, for example. 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 true in your comment on the review. res.end(); should be inside the callback, just below your write calls.

+50
Feb 12 '13 at 15:19
source

I get this error message and it mentions options.body

I had it 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 now it works without error message ... seems like an error though

+45
Dec 20 '15 at 7:34
source

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

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

One way to convert something to a string is:

 res.write( "" + obj ); 

whenever you try to send something. Another way is to call the .toString() method:

 res.write( obj.toString( ) ); 

Please note that this may still 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 an asynchronous operation. If this happens, then res.end(); will be called before any entry, i.e. Any record will fail anyway (because the connection will be closed at this point). Move this line to 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( ); }); 
+13
Feb 12 '13 at 15:20
source

if u wants to write a JSON object in response, change the content type of the header 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(); 
+1
Apr 26 '17 at 4:38 on
source



All Articles