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

前端 未结 8 1935
难免孤独
难免孤独 2020-12-24 11:29

I\'m simply trying to create a node server that outputs the HTTP status of a given URL.

When I try to flush the response with res.write, I get the error: throw new T

8条回答
  •  天命终不由人
    2020-12-24 11:54

    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.

提交回复
热议问题