npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

subrequests-express

v3.3.2

Published

Integrates Subrequests in your Express project

Downloads

118

Readme

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.