zhfsxtx
V2EX  ›  问与答

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

  •  
  •   zhfsxtx · Mar 26, 2013 · 3286 views
    This topic created in 4988 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
    Livid
    MOD
    PRO
    Mar 26, 2013
    1
    今天我们因为受到攻击,确实不太稳定。

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

    if (response .statusCode != 200) {
    console.log("unnormal statusCode: " + response .statusCode);
    return;
    };
    zhfsxtx
    zhfsxtx
    OP
    Mar 27, 2013
    4
    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   ·   Privacy   ·   Solana   ·   903 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 22:03 · PVG 06:03 · LAX 14:03 · JFK 17:03
    ♥ Do have faith in what you're doing.