Socket
Socket
Sign inDemoInstall

@springernature/backend-proxy

Package Overview
Dependencies
Maintainers
11
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@springernature/backend-proxy

Proxies frontend requests to a backend and can render the result


Version published
Weekly downloads
16
increased by23.08%
Maintainers
11
Weekly downloads
 
Created
Source

backend-proxy

Express/Connect middleware that proxies requests to a backend and renders the result.

Version Node.js version support Build status MIT licensed

Installation

To add backend-proxy to your project run

npm install --save @springernature/backend-proxy

Middleware

backendProxy(options)

The backend-proxy middleware will take all incoming HTTP requests and forward them to a backend service. The backend response will then be stored on the original HTTP request to be used by your application, or automatically rendered using render-backend-response.

const {backendProxy} = require('@springernature/backend-proxy');

// Proxy all requests to a backend
app.use('*', backendProxy({backend: 'http://my.backend'}));
app.get('*', (req, res) => res.json(req.backendResponse));

// Proxy a specific route to a specific backend
app.use('/login', backendProxy({
    backend: 'http://other.backebd/login',
    usePath: false
}), (req, res) => {
	res.render('login', req.backendResponse);
});

The following table describe the properties of the options object.

PropertyDescriptionTypeDefault
backendBackend service to proxy requests tostring
requiredContentTypeBackend response content type thats required to allow interception and deserializationstringapplication/json
usePathShould the incoming HTTP request's path be apended to the backend URLbooleantrue
changeHostSet the host header on the backend HTTP request to the backend hostbooleanfalse
keyThe property on the request object that the backend response will be stored under.stringbackendResponse

renderBackendResponse(options)

The renderBackendResponse renders any request which has a backendResponse on it. The backend response needs to contain a field named $config (the name can be changed) which contains the template to render, and layout if needed.

const {backendProxy, renderBackendResponse} = require('@springernature/backend-proxy');

app.use('*', backendProxy({
    backend: 'http://my.backend'
}));
app.use(renderBackendResponse());

Example backend response from http://my.backend to render the home template.

{
  "$config": {
    "template": "home"
  },
  "someField": "some value"
}

The following table describe the properties of the options object.

PropertyDescriptionTypeDefault
templateKeyThe property on the backend response that contains the template named (and layout)string$config
keyThe property on the request object that the backend response will be stored under.stringbackendResponse

mockBackendResponse(options)

Development only middleware that will match incoming requests to a json file, and if found will store it on the request under backendResponse simulating what backendProxy achieves. Note: This middleware will throw an exception if it is run in production

const {mockBackendResponse, renderBackendResponse} = require('@springernature/backend-proxy');
app.use(mockBackendResponse({
    directory: path.resolve(__dirname, 'backend-mocks')
}));
app.use(renderBackendResponse());

The following table describe the properties of the options object.

PropertyDescriptionTypeDefault
directoryDirectory to look for mock files instring
keyThe property on the request object that the backend response will be stored under.stringbackendResponse
File structure

The mockBackendResponse middleware will match incoming requests to files that match $PATH_$METHOD.js or $PATH_$METHOD.json. If both a .js and .json file exist then the .js file will be used.

.
|___ get.json  # Matches the root request of http://localhost:8080/
|___ login-get.json # Matches an HTTP GET to http://localhost:8080/login
|___ logout-get.js # Matches an HTTP GET to http://localhost:8080/logout
|___ login-post.json # Matches an HTTP POST to http://localhost:8080/login
|___ sub-directory
  |____ other-get.json # Matches an HTTP GET to http://localhost:8080/sub-directory/other

Examples

A set of examples can be found can be examples directory.

Change Log

History

Support

You can have a look at the Springer Nature Frontend Playbook for an explanation of how we support our open source projects.

License

MIT

Copyright © 2019 Springer Nature

Keywords

FAQs

Package last updated on 03 Feb 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc