48836

Can I use an http.ServerResponse as a prototype in node.js?

Question:

I am unable 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?

Answer1:

From <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/create" rel="nofollow">MDC</a>:

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 <strong>is NOT</strong> the old one.

At: <a href="https://github.com/ry/node/blob/a0159b4b295f69e5653ef96d88de579746dcfdc8/lib/http.js#L589" rel="nofollow">https://github.com/ry/node/blob/a0159b4b295f69e5653ef96d88de579746dcfdc8/lib/http.js#L589</a>

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 <strong>old</strong> 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.

Recommend

  • Convert XML to/from JSON in Java (without extra and elements)
  • as3 calling jQuery function
  • iPhone testing: testing IBOutlets
  • REGEX to capture sentences with quotes
  • java socket timeout behaviour
  • Unable to retrieve full text of title from JPEG metadata
  • xcode 4.3.2 process hangs
  • Simple command-line app I/O in Dart
  • Can't configure nginx as a proxy for tomcat with context path
  • What is wrong in my MVC implementation?
  • Zeromq with python hangs if connecting to invalid socket
  • @tailrec why does this method not compile with 'contains a recursive call not in tail position&
  • Unix Network Programming Clarification
  • SQL Query - Table Joining Problems
  • npm 5.4.1 install/uninstall all failing
  • Zurb Foundation _global.scss meta styles for js?
  • Apache 2.4 and php-fpm does not trigger apache http basic auth for php pages
  • Resize panoramic image to fixed size
  • How to add date and time under each post in guestbook in google app engine
  • TFS: Get latest causes slow project reloading
  • Volusion's generic SQL folder, functionality
  • Fill an image in a square container while keeping aspect ratio
  • MySQL WHERE-condition in procedure ignored
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Symfony2: How to get request parameter
  • Weird JavaScript statement, what does it mean?
  • jquery mobile loadPage not working
  • Web-crawler for facebook in python
  • How to get icons for entities from eclipse?
  • trying to dynamically update Highchart column chart but series undefined
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do you join a server to an Active Directory (domain)?
  • coudnt use logback because of log4j
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • JaxB to read class hierarchy
  • Binding checkboxes to object values in AngularJs
  • java string with new operator and a literal