简体   繁体   中英

Node.js: Serve JSON document

user = https.request options, (res) ->
        data = ''
        res.on 'data', (chunk) ->
            data += chunk
            return
        res.on 'end', ->
            if res.statusCode is 200
                console.log "oops?"
                usr = JSON.parse(data)
                console.log usr.status

            else
                console.log "oops2?"
                new Error "Response status code: " + res.statusCode
            return
        return
    user.end()
    res.write usr

I want to return the JSON document but I am unable to.

Thanks

First of all, I guess you should attempt some thing basic in plain javascript first before jumping to cs!

Solution, Here res.write usr should be right after usr = JSON.parse(data) !! Also, you should set res.contentType('application/json');

http://expressjs.com/ is recommended if you are really going to grow your code beyond this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM