Javascript 如何将req、res传递给回调函数?

Javascript 如何将req、res传递给回调函数?,javascript,node.js,express,asynchronous,Javascript,Node.js,Express,Asynchronous,在node.js中,如何为回调函数传递req、res 比如说, router.get("/", function (req, res) { var content = ''; fs.readFile("./json/hello.json", function(err, file) { if(err) res.render("index", {

在node.js中,如何为回调函数传递req、res

比如说,

router.get("/", function (req, res) {
    var content = '';

    fs.readFile("./json/hello.json", function(err, file)
    {
        if(err)
            res.render("index", {
                json: content
            });
        else
        {
            content = JSON.parse(file);
            res.render("index", {
                json: content.name
            });
        }
    });

});
它工作得很好。但是代码很难查找,因为有很多缩进。所以我想这样做

router.get("/", function (req, res) {
    fs.readFile("./json/hello.json", root_readFileCallBack());

});
function root_readFileCallBack(err, file)  {
    if (err) {
        res.render("index", {
            json: content
        });
    }
    else {
        content = JSON.parse(file);
        res.render("index", {
            json: content.name
        });
    }
}
上面的代码更易于阅读。但这会导致无法从“res”变量中找到“render”的错误

我试图将req、res作为参数传递,但效果不好


如何将req、res传递给回调函数?

将req/res参数附加到回调函数

router.get(“/”,函数(req,res){
fs.readFile(“./json/hello.json”,root_readFileCallBack.bind(this,req,res));
});
函数root\u readFileCallBack(req、res、err、file){
如果(错误){
res.render(“索引”{
json:内容
});
}
否则{
content=JSON.parse(文件);
res.render(“索引”{
json:content.name
});
}
}

将req/res参数附加到回调函数

router.get(“/”,函数(req,res){
fs.readFile(“./json/hello.json”,root_readFileCallBack.bind(this,req,res));
});
函数root\u readFileCallBack(req、res、err、file){
如果(错误){
res.render(“索引”{
json:内容
});
}
否则{
content=JSON.parse(文件);
res.render(“索引”{
json:content.name
});
}
}

创建一个闭包函数,该函数将为
readFile
函数返回一个回调函数,该函数的参数为
res
对象

router.get("/", function (req, res) {
  fs.readFile("./json/hello.json", root_readFileCallBack(res));
});

function root_readFileCallBack(res) {
  return function (err, file) {
    if (err) {
      res.render("index", {
        json: content
      });
    }
    else {
      content = JSON.parse(file);
      res.render("index", {
        json: content.name
      });
    }
  }
}

创建一个闭包函数,该函数将返回
readFile
函数的回调函数,该函数的参数为
res
对象

router.get("/", function (req, res) {
  fs.readFile("./json/hello.json", root_readFileCallBack(res));
});

function root_readFileCallBack(res) {
  return function (err, file) {
    if (err) {
      res.render("index", {
        json: content
      });
    }
    else {
      content = JSON.parse(file);
      res.render("index", {
        json: content.name
      });
    }
  }
}

@hoangdv有一个在实践中普遍使用的很好的答案。创建这样的工厂函数是一个很有用的技巧

这里有另一种方法来实现你想要的

router.get("/", function (req, res) {
    const callback = (err, file) => root_readFileCallBack(err, file, res)
    fs.readFile("./json/hello.json", callback);

});
function root_readFileCallBack(err, file, res)  {
    if (err) {
        res.render("index", {
            json: content
        });
    }
    else {
        content = JSON.parse(file);
        res.render("index", {
            json: content.name
        });
    }
}
基本上,我们让root_readFileCallBack()接受一个res参数,然后在router.get()中,我们包装root_readFileCallBack来稍微修改它的行为——具体地说,每当调用新回调时,我们都会使res自动传入


这是在使用,但是一个普通函数也可以很好地工作。

@hoangdv有一个很好的答案,这在实践中是常用的。创建这样的工厂函数是一个很有用的技巧

这里有另一种方法来实现你想要的

router.get("/", function (req, res) {
    const callback = (err, file) => root_readFileCallBack(err, file, res)
    fs.readFile("./json/hello.json", callback);

});
function root_readFileCallBack(err, file, res)  {
    if (err) {
        res.render("index", {
            json: content
        });
    }
    else {
        content = JSON.parse(file);
        res.render("index", {
            json: content.name
        });
    }
}
基本上,我们让root_readFileCallBack()接受一个res参数,然后在router.get()中,我们包装root_readFileCallBack来稍微修改它的行为——具体地说,每当调用新回调时,我们都会使res自动传入


这是使用了,但是一个普通函数也可以正常工作。

res/req仍然没有在
root\u readFileCallBack
@JaromandaX-Whoops中定义,你是对的,我误读了代码…res/req仍然没有在
root\u readFileCallBack
@JaromandaX-Whoops中定义,你是对的,我误读了代码…使用箭头函数语法,你可以用更少的缩进来做这件事:
constroot\u readFileCallBack=res=>(err,file)=>{…}
也会做同样的事情。使用箭头函数语法,你可以用更少的缩进来做这件事:
constroot\u readFileCallBack=res=>(err,file)=>{…}
也会做同样的事情。