V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
zhfsxtx
V2EX  ›  问与答

今天晚上V2EX好像挂了一会,我的公众号给挂了

  •  
  •   zhfsxtx · Mar 26, 2013 · 2938 views
    This topic created in 4787 days ago, the information mentioned may be changed or developed.
    看LOG是解析返回的JSON有问题。
    var topics = JSON.parse(buf.toString());
    SyntaxError: Unexpected end of input
    at Object.parse (native)
    at IncomingMessage.<anonymous> (/root/node/3w/weixin/v2ex/routes/handler.js:24:23)
    at IncomingMessage.EventEmitter.emit (events.js:92:17)
    at emitReadable_ (_stream_readable.js:382:10)
    at emitReadable (_stream_readable.js:377:5)
    at onEofChunk (_stream_readable.js:357:5)
    at readableAddChunk (_stream_readable.js:129:5)
    at IncomingMessage.Readable.push (_stream_readable.js:112:10)
    at Socket.socketCloseListener (http.js:1462:9)
    at Socket.EventEmitter.emit (events.js:117:20)
    at TCP.close (net.js:436:12)

    因为一个JSON错导致整个APP挂不应该啊,在NODEJS 里面是怎么做容错的啊。
    4 replies    1970-01-01 08:00:00 +08:00
    Livid
        1
    Livid  
    MOD
    PRO
       Mar 26, 2013
    今天我们因为受到攻击,确实不太稳定。

    你的程序中,应该检测从 V2EX 返回的 API Response 的 http status code,如果不是 200,那么肯定有问题。
    zhfsxtx
        2
    zhfsxtx  
    OP
       Mar 26, 2013
    @Livid 知道了
    zhfsxtx
        3
    zhfsxtx  
    OP
       Mar 27, 2013
    @Livid 刚回头看了下代码,里面已经有加检测http status code的代码了,但还 是挂了。

    if (response .statusCode != 200) {
    console.log("unnormal statusCode: " + response .statusCode);
    return;
    };
    zhfsxtx
        4
    zhfsxtx  
    OP
       Mar 27, 2013
    20 response .on('readable', function () {
    21 var buf = response .read(1024*1024);
    22 if(buf === null) return;
    23
    24 var topics = JSON.parse(buf.toString());
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2894 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 06:47 · PVG 14:47 · LAX 23:47 · JFK 02:47
    ♥ Do have faith in what you're doing.