subrequests-express
Integrates Subrequests in your Express project
Last updated 3 years ago by e0ipso .
GPL-2.0 · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install subrequests-express 
SYNC missed versions from official npm registry.

Subrequests Express

Coverage Status Known Vulnerabilities Commitizen friendly Greenkeeper badge Build Status

Usage

On the Express Server

To support subrequests in your express server you only need to add it to your router.

// 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:

// 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 and the ExpressRequestor are good examples. You can provide a custom requestor to subrequests-express by attaching your requestor object to req.subrequestsRequestor.

// 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:

// 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 for more information on how to use Subrequests.

Current Tags

  • 3.3.1                                ...           latest (2 years ago)

26 Versions

  • 3.3.1                                ...           2 years ago
  • 3.3.0                                ...           2 years ago
  • 3.2.1                                ...           2 years ago
  • 3.2.0                                ...           2 years ago
  • 3.1.1                                ...           3 years ago
  • 3.1.0                                ...           3 years ago
  • 3.0.2                                ...           3 years ago
  • 3.0.1                                ...           3 years ago
  • 3.0.0                                ...           3 years ago
  • 2.0.0                                ...           3 years ago
  • 1.13.0                                ...           3 years ago
  • 1.12.1                                ...           3 years ago
  • 1.12.0                                ...           3 years ago
  • 1.11.0                                ...           3 years ago
  • 1.10.1                                ...           3 years ago
  • 1.10.0                                ...           3 years ago
  • 1.9.0                                ...           3 years ago
  • 1.8.0                                ...           3 years ago
  • 1.7.0                                ...           3 years ago
  • 1.6.0                                ...           3 years ago
  • 1.5.0                                ...           3 years ago
  • 1.4.0                                ...           3 years ago
  • 1.3.0                                ...           3 years ago
  • 1.2.0                                ...           3 years ago
  • 1.1.0                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 2
Dependencies (5)
Dev Dependencies (13)
Dependents (1)

Copyright 2014 - 2017 © taobao.org |