Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.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
Node.js 定义我的模块I';我负责,用户应该注意哪些方面_Node.js_Request - Fatal编程技术网

Node.js 定义我的模块I';我负责,用户应该注意哪些方面

Node.js 定义我的模块I';我负责,用户应该注意哪些方面,node.js,request,Node.js,Request,我正在使用模块创建一个简单的端点包装器,其中一个端点用于上载文件和JSON,为此,我使用了多部分/表单数据: function doSomething(files, theJSON) { request.post('https://m.com/endpoint', { formData: { files, theJSON } }) } 但我怀疑提供正确论据的责任 这些文件必须是读取流的数组,而theJSON必须是json。在这里,我有两个选择:

我正在使用模块创建一个简单的端点包装器,其中一个端点用于上载文件和JSON,为此,我使用了
多部分/表单数据

function doSomething(files, theJSON) {
  request.post('https://m.com/endpoint', {
    formData: {
      files,
      theJSON
    }
  })
}
但我怀疑提供正确论据的责任

这些文件必须是读取流的数组,而theJSON必须是json。在这里,我有两个选择:

1-解析参数:

function doSomething(files, theJSON) {
  request.post('https://m.com/endpoint', {
    formData: {
      files: files.map(file => fs.createReadStream(file),
      theJSON: JSON.stringify(theJSON)
    }
  })
}
2-让用户负责正确的类型

哪一个是最好的??有正确的选择吗?

无论你做什么,都要记录下来。
为非常常见的任务(如上传文件)提供捷径有一些先例。在这种情况下,您可以有条件地检查字符串并将其视为路径,否则假设它是一个流:

files: files.map(file =>
    typeof file === 'string' ?
    fs.createReadStream(file) :
    file
)