javascript - 将参数传递给回调 node.js

标签 javascript node.js asynchronous callback

我正在 Node.js 中开发一个 RESTful 应用程序,我正在使用 https 库实现 http 请求。

目前,每个文件都包含一个带有特定参数的 http 请求,如下面的代码所示:

//test.js

var https = require('https');

module.exports.httpResponse = function (callback) {

    var options = {
      host: 'api.github.com',
      path: '/users/Guilherme-Routar',
      method: 'GET',
      //headers: {'user-agent': userAgent} 
    }

    var str = '';

    var request = https.request(options, function (response) {

        response.on('data', function (body) {
            str += body;
        });

        response.on('end', function () {
            return callback(str);
        });
    });

    request.on('error', (e) => {
        console.log(e);
    });

    request.end();
}

现在我想将 http 请求本身封装在一个单独的文件中(出于重构目的),以便每个文件都将调用模板并将其自己的参数传递给它。但这就是问题所在。是否可以将参数传递给回调?

//test.js
var https = require('https');

//I tried adding 'options' next to the 'callback' parameter
module.exports.httpResponse = function (callback, options) {

    var str = '';
    var request = https.request(options, function (response) {

        response.on('data', function (body) {
            str += body;
        });
        response.on('end', function () {
            return callback(str);
        });
    });
    request.on('error', (e) => {
        console.log(e);
    });
    request.end();
}

在另一个文件中,我将定义并传递函数的参数

//user.js    

var test = require('../test.js');

var options = {
  host: 'api.github.com',
  path: '/users/Guilherme-Routar',
  method: 'GET',
  //headers: {'user-agent': userAgent} 
}

// Passing 'options' as a parameter 
test.httpResponse(function(response, options) {
  console.log('response = ' + response);
})

但这显然行不通。你有什么建议可以给我吗?提前致谢。

最佳答案

似乎您想在回调后将选项作为附加参数传递,而不是期望它在回调内传递。

代替:

test.httpResponse(function(response, options) {
  //                                 ^ you don't want option to be part of the callback
  console.log('response = ' + response);
})

你想要:

test.httpResponse(function(response) {
  console.log('response = ' + response);
}, options)
// ^ pass options as second parameter

正如 Bergi 在下面提到的,Node 中通常的约定是将回调作为最后一个参数传递(正如您在使用的 https.request 方法中看到的那样),这需要您翻转您的 httpResponse 方法的参数:

module.exports.httpResponse = function (options, callback) {
// ...                                  ^^^^^^^^^^^^^^^^^ flip these two so that callback is at the end
}

然后使用它:

test.httpResponse(options, function(response) {
//                ^ pass options as first parameter
  console.log('response = ' + response);
})

关于javascript - 将参数传递给回调 node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47317317/

相关文章:

javascript - 代码在控制台中工作但不在终端中工作

javascript - 在 Internet Explorer 中更改 onchange 处理程序中的 tabindex 时出现奇怪的行为

ios - SDWebImage 库 - 来自 URL 的占位符

node.js 扩展 foo 不是一个函数

javascript - 在 Jade 和 Javascript 中动态创建具有不同 id 的模态

javascript - jQuery 突出显示滚动上的导航链接不起作用

Javascript椭圆点乘算法

javascript - 阻止 Firefox 退出延迟对象的执行

javascript - 如何将 javascript 日期传递给 laravel 4 API 时间戳

javascript - 如何(有效地)将标题添加到由 javascript 填充的 Bootstrap 轮播