خرید بک لینک

Vote count: 0

I am trying to use a curried fn with a closure as a callback to the servers request event.

It works the first time a client makes a request (the html is sent and diplayed) but the second time the page is blank. Does node erase the closure after calling the cb once?

// server.js
var pype = function(self, arr, errorHandler, cb) {
  var i = 0,
  req,
  res,
  run = function() {
    arr[i].call(self, req, res, next);
  },
  next = function(err) {
    if (err && errorHandler) {
      retu errorHandler.call(self, err, req, res);
    }
    i++
    if (i < arr.length) {
      retu run();
    } else if (cb) {
      retu cb.call(self, req, res);
    }
  }

  retu function(request, response) {
    req = request;
    res = response;
    run();
  }
};

var http = require("http”);
var server = http.createServer();
var errorhandler = function(err, req, res) {
  res.end(err);
},
var finalHandler = function(req, res) {
  res.setHeader("Content-Type", "text/html");
  res.end(req.html);
};
var requestHandler = pype(null, [pens, gits, concat, index, sort, html, finalHandler], errorhandler);

// this works only once
server.on('request', requestHandler);

// this works for multiple calls
server.on('request', function(req, res){
  var requestHandler = pype(null, [pens, gits, concat, index, sort, html, finalHandler], errorhandler);
  requestHandler(req, res);
});

Thanks!

asked 57 secs ago

برچسب: نویسنده: استخدام کار تاريخ: يکشنبه 20 تير 1395 ساعت: 20:32

صفحه بندی