title: nodejs - first argument must be a string or Buffer - when using response.write with http.request
body: <p>I'm simply trying to create a node server that outputs the HTTP status of a given URL.</p>
  
  <p>When I try to flush the response with res.write, I get the error: throw new TypeError('first argument must be a string or Buffer');</p>
  
  <p>But if I replace them with console.log, everything is fine (but I need to write them to the browser not the console).</p>
  
  <p>The code is</p>
  
  <pre><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);
  </code></pre>
  
  <p>I believe I should add a callback somewhere but pretty confused and any help is appreciated.</p>