[英]Correct way of using express middleware?
I have a question regarding express (connect) middleware. 我有一个关于快速(连接)中间件的问题。 What i'm trying to do is downloading DoubleClick Bid Manager Reports, parse and process them into my own MongoDB database. 我正在尝试下载DoubleClick Bid Manager报表,解析并将其处理到我自己的MongoDB数据库中。
My route looks as following: 我的路线如下:
app.route('/v1/spends/')
.get(dbmPolicy.isAllowed, buckets.read, buckets.check, reports.create, buckets.process, reports.update);
Where buckets.read
reads files from Google Cloud Storage, buckets.check
checks if report has already been processed into MongoDB, reports.create
creates the report that holds the metadata of the csv. 在buckets.read
从Google Cloud Storage读取文件的位置, buckets.check
检查报告是否已被处理到MongoDB中, reports.create
创建包含CSV的元数据的报告。 buckets.process
processes the data that resides inside of the csv and reports.update
updates the previously created report if all went succesfull. buckets.process
处理驻留在csv中的数据,如果全部成功,则reports.update
更新先前创建的报告。
As I find it very difficult to test the above process, I'm starting to doubt whether this is the correct way to implement the chain of processes. 由于发现测试上述过程非常困难,我开始怀疑这是否是实现过程链的正确方法。 If this is the correct way, how do I test each middleware function individually on it's behaviour? 如果这是正确的方法,我如何就其行为分别测试每个中间件功能?
Regards, 问候,
You may want to look into the Async package and especially the waterfall method. 您可能需要研究Async包,尤其是Waterfall方法。 That way you can run something like: 这样,您可以运行以下内容:
app.get('/v1/spends', function(req, res) {
async.waterfall([
dbmPolicy.isAllowed,
buckets.read,
buckets.check,
reports.create,
buckets.process,
reports.update
], function (err, result) {
if (err) res.status(500).send(err);
res.status(200).send(result);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.