Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 节点jsweb服务器中的功能管道_Javascript_Node.js - Fatal编程技术网

Javascript 节点jsweb服务器中的功能管道

Javascript 节点jsweb服务器中的功能管道,javascript,node.js,Javascript,Node.js,我正在开发一个函数管道,有点类似于带有use()的Connect/Express请求管道 请求处理程序通过使用.use()函数添加的函数堆栈运行。Async,作为一个n()函数必须被调用才能继续 使用熟悉的(req、res、next)调用函数。这是可行的,但它的最后一次添加是先执行的,这让我感到困惑,我想让它先添加后执行。这适用于后进,并在整个管道中继续r,s引用 (在这些示例中,我使用r,s而不是req,res.) 这里有一个FIFO的尝试。但它不起作用。想法 var chain = func

我正在开发一个函数管道,有点类似于带有use()的Connect/Express请求管道

请求处理程序通过使用.use()函数添加的函数堆栈运行。Async,作为一个n()函数必须被调用才能继续

使用熟悉的(req、res、next)调用函数。这是可行的,但它的最后一次添加是先执行的,这让我感到困惑,我想让它先添加后执行。这适用于后进,并在整个管道中继续r,s引用

(在这些示例中,我使用r,s而不是req,res.)

这里有一个FIFO的尝试。但它不起作用。想法

var chain = function(r, s, n){
    console.log(1, arguments);
    n();
};

function use(f){
    var th = chain;
    chain = (function(nxt){
        return function(r, s){
            th(r, s, nxt);
        }
    })(f);
}

use(function(r, s, n){
    console.log(2, arguments)
    n();
})

use(function(r, s, n){
    console.log(3, arguments)
    n();
})

chain(0,1);

我想要一些不使用循环来运行函数的东西。

我不知道您想要复制什么,但是可以作为一个好的起点吗

var chain = function(fn) {
    var queue = fn ? [fn] : [], index = 0, req, res;
    var run = function(r, s) {
        req = req || r; res = res || s;
        if (index < queue.length) {
            queue[index++](req, res, run);
        }    
    };

    return {
        use: function(fn) {
            queue.push(fn);    
        },
        run: run
    };
};

var c = chain();
c.use(function(r, s, n) {log(r, s, 1); n();});
c.use(function(r, s, n) {log(r, s, 2); n();});
c.use(function(r, s, n) {log(r, s, 3); n();});
c.use(function(r, s, n) {log(r, s, 4); n();});

c.run("req", "res");

//=> req, res, 1
//   req, res, 2
//   req, res, 3
//   req, res, 4
var链=函数(fn){
var queue=fn?[fn]:[],index=0,req,res;
var run=函数(r,s){
req=req | | r;res=res | | s;
if(索引<队列长度){
队列[index++](请求、恢复、运行);
}    
};
返回{
用途:功能(fn){
队列推送(fn);
},
跑:跑
};
};
var c=链();
c、 使用(函数(r,s,n){log(r,s,1);n();});
c、 使用(函数(r,s,n){log(r,s,2);n();});
c、 使用(函数(r,s,n){log(r,s,3);n();});
c、 使用(函数(r,s,n){log(r,s,4);n();});
c、 运行(“请求”、“恢复”);
//=>req,res,1
//请求,res,2
//请求,res,3
//请求,res,4
如果愿意,还可以在对
链的初始调用中提供函数

var chain = function(fn) {
    var queue = fn ? [fn] : [], index = 0, req, res;
    var run = function(r, s) {
        req = req || r; res = res || s;
        if (index < queue.length) {
            queue[index++](req, res, run);
        }    
    };

    return {
        use: function(fn) {
            queue.push(fn);    
        },
        run: run
    };
};

var c = chain();
c.use(function(r, s, n) {log(r, s, 1); n();});
c.use(function(r, s, n) {log(r, s, 2); n();});
c.use(function(r, s, n) {log(r, s, 3); n();});
c.use(function(r, s, n) {log(r, s, 4); n();});

c.run("req", "res");

//=> req, res, 1
//   req, res, 2
//   req, res, 3
//   req, res, 4