您的位置:首页 > Web前端 > Node.js

node tail 日志服务

2014-01-27 15:00 190 查看
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,没有任何响应
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: