I am una开发者_JAVA技巧ble to get this to work:
var proxyResponse = function(res) {
return Object.create(res);
};
Calling the standard response methods on the object returned by this method doesn't work, for example:
http.createServer(function(req, res) {
res = proxyResponse(res);
res.writeHead(200, {"Content-Type": "text/html"});
res.end("Hallelujah! (Praise the Lord)");
}).listen(8080);
The server just hangs. Can someone explain what I'm doing wrong?
From MDC:
Object.create(proto [, propertiesObject ])
This create a new object which's prototype is proto, the object itself has nothing define on itself:
res.foo = function() {
console.log(this);
}
res.foo();
res = proxyResponse(res);
res.foo();
Result:
{ socket:
{ fd: 7,
type: 'tcp4',
allowHalfOpen: true,
_readWatcher:
{ socket: [Circular],
....
{}
So why doesn't it throw a error and explodes? Besides the messed up property lookup and setting, there's one reason that it doesn't work.
While your new object references all the same Object as the old one, itself is NOT the old one.
At: https://github.com/ry/node/blob/a0159b4b295f69e5653ef96d88de579746dcfdc8/lib/http.js#L589
if (this.output.length === 0 && this.connection._outgoing[0] === this) {
This finishes the request, this
is the new object, but this.connection._outgoing[0]
still references the old object, so the request is never finished and the server hangs.
I still have no idea what you're trying to achieve here anyways, since it doesn't make sense to use Object.create
here, if you're worried about res being overwritten in case of another requests, that's not the case since each res is it's own variable which references a different object.
精彩评论