我有面向express
的示例代码,用于从提供程序接收多部分表单数据:
const multer = require('multer');
const callback = multer();
app.post('/callback', callback.single('json'), (req, res) => {
res.status(200);
res.send('API Event Received');
const data = req.body.json;
const events = JSON.parse(data);
const eventType = events.event.event_type;
const signatureRequestId = events.signature_request.signature_request_id;
switch(eventType) {
case 'signature_request_sent':
console.log(`Signature request ${signatureRequestId} has been sent.`);
break;
case 'signature_request_viewed':
console.log(`Signature request ${signatureRequestId} has been viewed.`);
break;
case 'signature_request_downloadable':
console.log(`Signature request ${signatureRequestId} is downloadable.`);
break;
case 'signature_request_signed':
console.log(`Signature request ${signatureRequestId} has been signed.`);
break;
case 'signature_request_declined':
console.log(`Signature request ${signatureRequestId} has been declined.`);
break;
default:
console.log('');
break;
}
});
在this SO post,上,我看到了一个将流星连接到Multer以接收图像文件的示例:
if (Meteor.isServer) {
Meteor.startup(function () {
multer({ dest: './uploads/',
rename: function (fieldname, filename) {
return filename+Date.now();
},
onFileUploadStart: function (file) {
console.log(file.originalname + ' is starting ...');
},
onFileUploadComplete: function (file) {
console.log(file.fieldname + ' uploaded to ' + file.path);
var fileName = file.name;
var done=true;
}
})
});
}
...但是我还没有弄清楚如何组合这两个代码示例。
在Meteor中使用Multer接收多部分表单数据的正确方法是什么?
更新:一位评论者问我到目前为止所做的尝试。我有以下流星代码,可创建常规的单部分REST端点:
WebApp.connectHandlers.use('/ReceiveCalls', (req, res, next) => {
console.log('received message');
console.log( req);
console.log(res);
res.writeHead(200);
});
上面显示的Multer示例包括一个用于接收图像的目标文件夹,但未显示如何创建端点。流星代码显示了如何创建REST端点,但是我还看不到如何将其与Multer代码结合使用。我敢肯定这很容易,但是我还没有弄清楚该怎么做。
答案 0 :(得分:0)
答案是via coagmano on the Meteor forum:
注意:如果对他人有用,则提供者为HelloSign。这是我完成的代码。
//TELL HELLOSIGN THE CALLBACK URL
const hellosign = require('hellosign-sdk')({key: 'xxxxx'});
hellosign.account.update({
callback_url: 'https://example.com/myCallBackURL'
}).then((res) => {
console.log('success setting hellosign account callback url');
}).catch((err) => {
console.log('error setting hellosign account callback url');
console.log(err)
});
//REST API FOR HELLOSIGN
const callback = multer({dest: './uploads'});
// Mount the middleware first so it's run first
WebApp.connectHandlers.use('/myCallBackURL', callback.single('json'));
// Then mount the user handler
WebApp.connectHandlers.use('/myCallBackURL', (req, res, next) => {
res.setHeader('Content-Type', 'text/html');
res.setHeader('X-Foo', 'bar');
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello API Event Received');
const data = req.body.json;
const events = JSON.parse(data);
const eventType = events.event.event_type;
const signatureRequestId = events.signature_request.signature_request_id;
switch (eventType) {
case 'signature_request_sent':
console.log(`Signature request ${signatureRequestId} has been sent.`);
break;
case 'signature_request_viewed':
console.log(`Signature request ${signatureRequestId} has been viewed.`);
break;
case 'signature_request_downloadable':
console.log(`Signature request ${signatureRequestId} is downloadable.`);
break;
case 'signature_request_signed':
console.log(`Signature request ${signatureRequestId} has been signed.`);
break;
case 'signature_request_declined':
console.log(`Signature request ${signatureRequestId} has been declined.`);
break;
default:
console.log('');
break;
}
});