UNPKG

2.59 kBMarkdownView Raw
1# Subrequests Express
2
3[![Coverage Status](https://coveralls.io/repos/github/e0ipso/subrequests-express/badge.svg)](https://coveralls.io/github/e0ipso/subrequests-express)
4[![Known Vulnerabilities](https://snyk.io/test/github/e0ipso/subrequests-express/badge.svg)](https://snyk.io/test/github/e0ipso/subrequests-express)
5[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
6[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
7[![Greenkeeper badge](https://badges.greenkeeper.io/e0ipso/subrequests-express.svg)](https://greenkeeper.io/)
8[![Build Status](https://travis-ci.org/e0ipso/subrequests-express.svg?branch=master)](https://travis-ci.org/e0ipso/subrequests-express)
9
10## Usage
11
12### On the Express Server
13
14To support subrequests in your express server you only need to add it to your router.
15
16```js
17// app.js
18const { SubrequestsRouter } = require('subrequests-express');
19
20// All your route declarations.
21// …
22
23// Add the request aggregator.
24router.use(SubrequestsRouter);
25```
26
27This will add a route in `/subrequests` that will process blueprints either by GET or POST requests.
28
29#### Customize the Route
30
31In order to customize the route of the request aggregator you will need to add it to your
32configuration object using `config`.
33
34Set the route in the configuration json/yaml for your environment.
35
36```yml
37subrequests:
38 route: '/:version/my-request-aggregator'
39```
40
41That will accept requests to `https://example.org/^v5.0.1/my-request-aggregator`, for instance.
42Note that you can include parameters that Express will process normally.
43
44#### Customize the Response Format
45
46You can provide a subresponse merged plugin by attaching it to the express request object under the
47`subrequestsResponseMerge` key. You can do something like:
48
49```js
50// app.js
51const { SubrequestsRouter } = require('subrequests-express');
52const JsonResponse = require('subrequests-json-merger');
53
54// All your route declarations.
55// …
56
57router.all(config.get('subrequests.route'), (req, res, next) => {
58 // Make sure that subrequests-json-merger merges responses using JSON.
59 req.subrequestsResponseMerger = JsonResponse;
60 next();
61});
62// Request aggregator.
63router.use(SubrequestsRouter);
64
65```
66
67### On the Consumer Application
68
69Use the request aggregator normally under `/subrequests` or the configured route. See
70[Subrequests](https://github.com/e0ipso/subrequests#readme) for more information on how to use
71Subrequests.