如何将2个端点express.js合并为一个新端点

时间:2019-02-20 07:41:47

标签: javascript node.js express

我有2个现有端点/balance /transactions 创建新的/balance-and-transactions而不重写现有代码的最佳方法是什么? 例如:

a('/balance', () => {
 foo()
 res.send({ /*some json with balance */ });
});

b('/transactions',() => {
 boo();
 res.send({ /*some json with transactions */ });
});


newendpoint('/balance-and-transactions', () => {
 foo();
 boo();
 res.send({ /*some combined json with balance and transactions altogether */ });
});

在newendpoint(内部调用)中调用2个请求是一个好主意吗?

 newendpoint('/balance-and-transactions', () => {
     data1 = request('/balance');
     data2 = request('/transactions');
    // then combine data1 and data2
     res.send({ /*some combined json with balance and transactions altogether */ });
    });

1 个答案:

答案 0 :(得分:0)

合并这些端点不是一个好主意,但是如果您仍然想这样做,最好使用破折号而不是下划线mix .js('resources/assets/js/app.js', 'public/js') .sass('resources/assets/sass/app.scss', 'public/css') .version();