通过 Apache 上的 Passenger 5.0.15 使用 Node.js,我正在寻找一种在 Node.js 内检索每个 HTTP 请求环境变量的方法http.IncomingMessage object https://nodejs.org/api/http.html#http_http_incomingmessage或任何替代方案。
An example of what I mean by a per-request environment variable, would be one set by Apache mod_rewrite. This is not a real-world example, and I'm aware any path-related details could be easily retrieved in the Node app. Apache modules may set environment variables for the request1:
# Apache sets an environment variable for this HTTP request
RewriteRule path/([^/]+) - [E=pathvar:$1]
在 Ruby 的架子上,输入环境被传递到request.env https://github.com/rack/rack/blob/master/lib/rack/request.rb#L23-L25。在 PHP 中它被传递到$_SERVER["pathvar"]
.
乘客确实尝试设置它们,但做得有些不正确通过为整个 Node.js 进程设置它们 https://github.com/phusion/passenger/issues/1479#issuecomment-127277646,与为其设置的 HTTP 请求无关。这可能会在 Passenger 中得到修复或更改,但不能帮助我找到每个请求的变量。
console.log(process.env.pathvar);
// Has the value, but has been set for the entire Node.js process
// Subsequent requests retain the SAME value due to Passenger's current implementation
// Environment variables aren't present
// in the http server request object
var s = http.createServer(function(request, response) {
// undefined, which makes sense because they're not headers
console.log(request.headers.pathvar);
});
- 每个 HTTP 请求的环境变量作为 Node.js 中的一个概念是否有意义,以便 Passenger 可以正确设置它们?
- 如果他们这样做,是否得到了支持
http
模块,以及如何检索它们?
- 如果不是由
http
module,有支持它们的中间件吗? (我发现没有提到环境变量Connect 的中间件集合中 https://github.com/senchalabs/connect#readme)
1The true use case we are trying to solve is retrieval of Shibboleth user authentication attributes, set by Apache mod_shib as environment variables similarly to how mod_rewrite would set them before invoking the Node.js application. Indeed this is the reason for involving Apache+Passenger in the first place. There is an available but undesirable workaround in Shibboleth configuration to send user attributes as HTTP headers instead. There are alternative SAML modules for Node.js as well, but current infrastructure already depends on Apache mod_shib.