我定义了这样的快速路线:
qiniuRoute.js
module.exports = function (app) {
const qiniu = require('../controllers/qiniuController')
app.route('/upload')
.post(qiniu.uploadFile);
}
qiniuController.js
function uploadFile(req, res) {
const form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
console.log('receive');
console.log(files.file);
const localFile = files.file.path;
const config = new qiniu.conf.Config();
const formUploader = new qiniu.form_up.FormUploader(config);
const putExtra = new qiniu.form_up.PutExtra();
const key = files.file.name;
const uploadToken = renderUploadToken(req, res);
// fileupload
formUploader.putFile(uploadToken, key, localFile, putExtra, function (respErr,
respBody, respInfo) {
if (respErr) {
throw respErr;
res.json({
error: respErr.error
})
return;
}
console.log(respInfo)
if (respInfo.statusCode == 200) {
console.log(respBody);
res.json(respBody)
} else {
console.log(respInfo.statusCode);
console.log(respBody);
}
});
});
};
但是当我发出文件上传请求时出现错误显示“发送后无法设置标头”,似乎我多次响应客户端但我已经返回错误句柄。我还有另一个猜测,导致我的fileupload是异步函数,这意味着我的回调函数不能res.json?