【发布时间】:2011-09-08 04:57:44
【问题描述】:
我正在制定一个巧妙的计划,其中涉及使用 node.js 作为另一个服务前面的代理服务器。
简而言之:
- 将传入请求分派到静态文件(如果存在)
- 否则,将请求分派给另一个服务
我有基本的工作,但现在试图让整个事情与 Sencha Connect 一起工作,这样我就可以访问所有提供的关键中间件。
所有的动作都发生在dispatchProxy下面
connect(
connect.logger(),
connect.static(__dirname + '/public'),
(request, response) ->
dispatchProxy(request, response)
).listen(8000)
dispatchProxy = (request, response) ->
options = {host: host, port: port, method: request.method, headers: request.headers, path: request.url}
proxyRequest = http.request(options, (proxyResponse) ->
proxyResponse.on('data', (chunk) ->
response.write(chunk, 'binary')
)
proxyResponse.on('end', (chunk) ->
response.end()
)
response.writeHead proxyResponse.statusCode, proxyResponse.headers
)
request.on('data', (chunk) ->
proxyRequest.write(chunk, 'binary')
)
# this is never triggered for GETs
request.on('end', ->
proxyRequest.end()
)
# so I have to have this here
proxyRequest.end()
您会在上面的最后一行看到 proxyRequest.end()。
我发现在处理 GET 请求时,请求的 END 事件永远不会被触发,因此需要调用 proxyRequest.end()。 POST 请求按预期触发 DATA 和 END 事件。
那么几个问题:
对 proxyRequest.end() 的调用安全吗?也就是说,即使在事件循环之外调用它,proxyResponse 仍然会完成吗?
GET 不触发 END 事件是否正常,或者 END 是否在连接堆栈中的某处被捕获?
【问题讨论】:
-
finish事件似乎按照here 的建议工作
标签: javascript proxy node.js connect coffeescript