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

前端 未结 8 1924
难免孤独
难免孤独 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

    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();
    
    0 讨论(0)
  • 2020-12-24 12:05

    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.

    0 讨论(0)
提交回复
热议问题