Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/e0ipso/subrequests-express
Integrates Subrequests in your Express project.
https://github.com/e0ipso/subrequests-express
Last synced: 9 days ago
JSON representation
Integrates Subrequests in your Express project.
- Host: GitHub
- URL: https://github.com/e0ipso/subrequests-express
- Owner: e0ipso
- License: gpl-2.0
- Created: 2017-08-18T14:53:22.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2022-12-31T07:10:19.000Z (almost 2 years ago)
- Last Synced: 2024-04-13T16:12:52.959Z (7 months ago)
- Language: JavaScript
- Size: 982 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Subrequests Express
[![Coverage Status](https://coveralls.io/repos/github/e0ipso/subrequests-express/badge.svg)](https://coveralls.io/github/e0ipso/subrequests-express)
[![Known Vulnerabilities](https://snyk.io/test/github/e0ipso/subrequests-express/badge.svg)](https://snyk.io/test/github/e0ipso/subrequests-express)
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
[![Greenkeeper badge](https://badges.greenkeeper.io/e0ipso/subrequests-express.svg)](https://greenkeeper.io/)
[![Build Status](https://travis-ci.org/e0ipso/subrequests-express.svg?branch=master)](https://travis-ci.org/e0ipso/subrequests-express)## Usage
### On the Express Server
To support subrequests in your express server you only need to add it to your router.
```js
// app.js
const { subrequestsRouterFactory } = require('subrequests-express');// All your route declarations.
// …
const app = express();const options = {};
// Add the request aggregator on the '/subrequests' route.
router.use(subrequestsRouterFactory('/subrequests', options, app));
```This will add a route in `/subrequests` that will process blueprints either by GET or POST requests.
#### Options
Subrequests is very useful when you are making internal requests. Without any options, subrequests
will use the hostname in the master request to issue relative requests. A request is considered
internal if it has a `uri` that it's a path instead of a fully qualified URL.- **`host`**: The host to use for internal requests. Ex: `localhost`.
- **`port`**: The port to use for internal requests. Ex: `3000`.#### Customize the Response Format
You can provide a subresponse merger plugin by attaching it to the express request object under the
`subrequestsResponseMerge` key. You can do something like:```js
// app.js
const { subrequestsRouterFactory } = require('subrequests-express');
const JsonResponse = require('subrequests-json-merger');// All your route declarations.
// …
const app = express();router.all('/subrequests', (req, res, next) => {
// Make sure that subrequests-json-merger merges responses using JSON.
req.subrequestsResponseMerger = JsonResponse;
next();
});
// Request aggregator.
router.use(subrequestsRouterFactory('/subrequests', {}, app));
```#### Customize the Way Subrequests Are Sent
You can customize the way subrequests are sent. For that you only need to write a _requestor_. The
[HttpRequestor](https://github.com/e0ipso/subrequests/blob/master/src/HttpRequestor.js) and the
[ExpressRequestor](https://github.com/e0ipso/subrequests-express/blob/master/src/ExpressRequestor.js)
are good examples. You can provide a custom requestor to `subrequests-express` by attaching your
requestor object to `req.subrequestsRequestor`.```js
// app.js
const { subrequestsRouterFactory, ExpressRequestor } = require('subrequests-express');
class CustomRequestor extends ExpressRequestor {
// …
}// All your route declarations.
// …
const app = express();router.all('/subrequests', (req, res, next) => {
// Make sure that subrequests-json-merger merges responses using JSON.
req.subrequestsRequestor = new CustomRequestor(req);
next();
});
// Request aggregator.
router.use(subrequestsRouterFactory('/subrequests', {}, app));
```#### Defaults for request
You can override properties of the generated request objects (IncomingMessage) by attaching it to
the express request object under the `subrequestsOptions.requestOptions` key. You can do something
like:```js
// app.js
const { subrequestsRouterFactory } = require('subrequests-express');// All your route declarations.
// …
const app = express();router.all('/subrequests', (req, res, next) => {
// Make sure that subrequests-json-merger merges responses using JSON.
req.subrequestsResponseMerger = JsonResponse;
next();
});
// Request aggregator.
router.use(
(req, res, next) => {
req.subrequestsOptions = {
requestOptions: { headers: { 'X-Powered-By': 'Subrequests'} },
};
next();
},
subrequestsRouterFactory(
'/subrequests',
{ host: 'localhost', port: 3000 },
app
)
);```
### On the Consumer Application
Use the request aggregator normally under `/subrequests` or the configured route. See
[Subrequests](https://github.com/e0ipso/subrequests#readme) for more information on how to use
Subrequests.