简体   繁体   English

Node.js:提供JSON文档

[英]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. 我想返回JSON文档,但我无法。

Thanks 谢谢

First of all, I guess you should attempt some thing basic in plain javascript first before jumping to cs! 首先,我想在跳转到cs之前,你应该首先在普通的javascript中尝试一些基本的东西!

Solution, Here res.write usr should be right after usr = JSON.parse(data) !! 解决方案,这里res.write usr应该在usr = JSON.parse(data)之后! Also, you should set res.contentType('application/json'); 另外,你应该设置res.contentType('application / json');

http://expressjs.com/ is recommended if you are really going to grow your code beyond this. 如果您真的要在此之外增加代码,建议使用http://expressjs.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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