node tail 日志服务

var http = require('http'),
    ,spawn = require('child_process').spawn


function onRequest(req, res) {
    var reqUrl = req.url    
    if ('/' == reqUrl || 'favicon.ico' == reqUrl) {
        return res.end('Im debugger')
        }

    res.writeHead(200 ,{'Content-Type': 'text/plain','Cache-Control': 'no-cache,no-store'})
        }
    var logFile = 'xxxxx'
    tailLog(logFile)
    req.on('close' ,function(){
        console.log('aborted')
        req.tail.kill('SIGHUP')
        })

    function tailLog(logFile) {
        //console.log(logFile)    
        logFile = logFile.trim()
        if (!logFile) return res.end('Log File lost')
        var tail  = req.tail = spawn('tail' , ['-f' , logFile])
        tail.stdout.on('data' , function(data){
            var line = data.toString('utf-8')
            console.log(line)    
            res.write(line)
            })
        }
}


var arguments = process.argv.splice(2)
http.createServer(onRequest).listen(arguments[0] || 2014)

服务通过 nginx 反向代理后 要注意 conf 里需要配置 proxy_buffering off;  不然 nginx 要一直等 node,没有任何响应

原文地址:https://www.cnblogs.com/vaal-water/p/3534888.html