coffeescript+nodejs实现http代理服务器支持get、post


http = require('http')
url = require('url')

http.globalAgent.maxSockets = 10240;

parseOpthons = (req) ->
opt = {}
url_info = url.parse(req.url)

opt.host = url_info.host
opt.hostname = url_info.hostname
opt.path = url_info.path
opt.port = 80
opt.method = req.method
opt.headers = req.headers
console.log opt.method
return opt

server = http.createServer (req, res) ->

opt = parseOpthons(req)

data = ""
req.on 'data', (post_data_chunk) ->
data = post_data_chunk

req.on 'end', () ->
if opt.method == "POST"
proxy_request.end(data)
else
proxy_request.end()

proxy_request = http.request opt, (p_res) ->
headers = p_res.headers
statusCode = p_res.statusCode
res.writeHead(statusCode, headers)

p_res.on 'data', (chunk) ->
res.write(chunk, 'binary')

p_res.on 'end', () ->
res.end()

responseHdr = () ->
if proxy_request
else
proxy_request.abort()

timeoutHdr = () ->
proxy_request.emit('req-timeout')

proxy_request.on 'req-timeout', responseHdr

proxy_request.on 'error', () ->
clearTimeout(setTimeout(timeoutHdr, 5000))

server.listen 8888

process.on 'uncaughtException', (err) ->
console.log('LAST ERROR: Caught exception: ' + err)
util.log(err.stack)

console.log "server start...."
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值