Javascript 重构npm请求方法代码

Javascript 重构npm请求方法代码,javascript,node.js,request,Javascript,Node.js,Request,目前正在使用npm请求,我希望将代码重构为以下内容: app.post('/work_order',function (req,res) { var work_order = req.body.work_order; var url = soapURI + work_order; reqMethod(url); }); app.get('/work_order/:work_order', function (req, res) { var work_order = req.pa

目前正在使用npm请求,我希望将代码重构为以下内容:

app.post('/work_order',function (req,res) {
  var work_order = req.body.work_order;
  var url = soapURI + work_order;
  reqMethod(url);
});

app.get('/work_order/:work_order', function (req, res) {
  var work_order = req.params.work_order;
  var url = soapURI + work_order;
  reqMethod(url);
});

function reqMethod (url){
  request({
    url: url,
    json: true
  }, function (error, response, body) {
    if (!error && response.statusCode === 200) {
      // console.log(body); // Print the json response
      res.send(body);
    }
  });
}
问题是,
reqMethod
现在是一个承诺,而
res.send(body)
是其中的一部分。我不能简单地
返回res.send(body)
。我在
.get
.post
中都有两段重复的代码(每段代码都与该方法完全相同),因此我想将它们移到一个通用的、可重用的方法中

问题:

  • 我怎样才能让它工作

  • request
    是一个好的npm模块吗?你能提出一些替代方案,也许更好的方案吗

  • 谢谢你

    你有两个选择

    选项1:将结果(res)参数传递到reqMethod:

    app.post('/work_order',function (req,res) {
      var work_order = req.body.work_order;
      var url = soapURI + work_order;
      reqMethod(url, res);
    });
    
    app.get('/work_order/:work_order', function (req, res) {
      var work_order = req.params.work_order;
      var url = soapURI + work_order;
      reqMethod(url, res);
    });
    function reqMethod (url, res){
      request({
        url: url,
        json: true
      }, function (error, response, body) {
        if (!error && response.statusCode === 200) {
          // console.log(body); // Print the json response
          res.send(body);
        }
        else {
          //send error here
        }
      });
    }
    
    选项2:向reqMethod添加回调

    app.post('/work_order',function (req,res) {
      var work_order = req.body.work_order;
      var url = soapURI + work_order;
      reqMethod(url, function(err, body) {
        res.send(body)
      );
    });
    
    app.get('/work_order/:work_order', function (req, res) {
      var work_order = req.params.work_order;
      var url = soapURI + work_order;
      reqMethod(url, function(err, body) {
        res.send(body)
      );
    });
    function reqMethod (url, cb){
      request({
        url: url,
        json: true
      }, function (error, response, body) {
        if (!error && response.statusCode === 200) {
          // console.log(body); // Print the json response
          cb(null, body);
        }
        else {
          cb(error)
        }
      });
    }
    
    你有两个选择

    选项1:将结果(res)参数传递到reqMethod:

    app.post('/work_order',function (req,res) {
      var work_order = req.body.work_order;
      var url = soapURI + work_order;
      reqMethod(url, res);
    });
    
    app.get('/work_order/:work_order', function (req, res) {
      var work_order = req.params.work_order;
      var url = soapURI + work_order;
      reqMethod(url, res);
    });
    function reqMethod (url, res){
      request({
        url: url,
        json: true
      }, function (error, response, body) {
        if (!error && response.statusCode === 200) {
          // console.log(body); // Print the json response
          res.send(body);
        }
        else {
          //send error here
        }
      });
    }
    
    选项2:向reqMethod添加回调

    app.post('/work_order',function (req,res) {
      var work_order = req.body.work_order;
      var url = soapURI + work_order;
      reqMethod(url, function(err, body) {
        res.send(body)
      );
    });
    
    app.get('/work_order/:work_order', function (req, res) {
      var work_order = req.params.work_order;
      var url = soapURI + work_order;
      reqMethod(url, function(err, body) {
        res.send(body)
      );
    });
    function reqMethod (url, cb){
      request({
        url: url,
        json: true
      }, function (error, response, body) {
        if (!error && response.statusCode === 200) {
          // console.log(body); // Print the json response
          cb(null, body);
        }
        else {
          cb(error)
        }
      });
    }
    

    Express请求处理程序需要在自己的作用域中使用
    res.send()
    ,或者需要将所需变量传递给助手函数,以便可以在那里使用
    res

    当它们被用作快速请求处理(而不是回调)的基础时,使用允许更简单的错误处理流

    该库提供了标准Promise功能的非常有用的超集。自4.x以来一直是native Node.js的一部分

    它完成了到目前为止我所需要的所有HTTP功能,而且它的重量比
    请求
    轻得多

    代码
    expressResponse()
    将Express组件与任何响应生成代码分开,以防我想将这些函数用于Express以外的其他功能

    reqMethod()
    从调用
    needle.get()
    并自动将回调转换为承诺的
    needle.getAsync()
    返回承诺

    Express请求处理程序方法签名中添加了一个
    next
    ,该签名也被传递到
    expressResponse()
    。然后将
    .catch(next)
    附加到承诺链。这将允许Express处理由承诺引发的任何未捕获的异常

    使用
    next()
    错误处理需要添加一个。您可以更清楚地意识到这一点,为不同的
    Error
    类生成不同的响应


    请注意,有了承诺,您现在可以像在
    reqMethod()
    中那样在异步承诺中安全地抛出
    正常异常,它们将由Express错误中间件处理。

    Express请求处理程序需要使用
    res.send()
    在它自己的范围内,或者需要将所需的变量传递给助手函数,以便在那里使用
    res

    当它们被用作快速请求处理(而不是回调)的基础时,使用允许更简单的错误处理流

    该库提供了标准Promise功能的非常有用的超集。自4.x以来一直是native Node.js的一部分

    它完成了到目前为止我所需要的所有HTTP功能,而且它的重量比
    请求
    轻得多

    代码
    expressResponse()
    将Express组件与任何响应生成代码分开,以防我想将这些函数用于Express以外的其他功能

    reqMethod()
    从调用
    needle.get()
    并自动将回调转换为承诺的
    needle.getAsync()
    返回承诺

    Express请求处理程序方法签名中添加了一个
    next
    ,该签名也被传递到
    expressResponse()
    。然后将
    .catch(next)
    附加到承诺链。这将允许Express处理由承诺引发的任何未捕获的异常

    使用
    next()
    错误处理需要添加一个。您可以更清楚地意识到这一点,为不同的
    Error
    类生成不同的响应


    请注意,通过承诺,您现在可以像在
    reqMethod()
    中那样在异步承诺中安全地抛出
    正常异常,它们将由Express error middleware处理。

    感谢您回答我的问题。这确实对我有帮助。谢谢你回答我的问题。这对我真的很有帮助。谢谢你的意见。当我看到蓝知更鸟时,我非常欣赏它,并通过Sequelize文档查看它。是的,我想看看蓝鸟的承诺是什么。您的回复启发了我对蓝鸟和针的认识(针是请求的一个很好的替代品)。我们一定会测试它。谢谢,谢谢你的意见。当我看到蓝知更鸟时,我非常欣赏它,并通过Sequelize文档查看它。是的,我想看看蓝鸟的承诺是什么。您的回复启发了我对蓝鸟和针的认识(针是请求的一个很好的替代品)。我们一定会测试它。谢谢