node tail 日志服务

本文介绍了一个使用Node.js创建简易服务器的例子,该服务器能够处理特定请求并实时显示日志文件的内容。通过nginx进行反向代理时,需注意配置以避免缓冲问题,确保实时数据流的正确处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值