0.1.2 • Published 6 years ago

stub-proxy-server v0.1.2

Weekly downloads
1
License
MIT
Repository
-
Last release
6 years ago

Stub-Proxy-Server

Installing

npm install --save stub-proxy-server express

or

yarn add stub-proxy-server express

Requirements

  • node.js > 4.0.0
  • express > 4.0.0

Description

This package allows you to stub endpoints of a server while still keeping the rest of the routes proxied.

An example use-case would be a lot easier edge-case handling in end-to-end tests.

Usage

const ProxyServer = require('stub-proxy-server');


function runServer () {
    // Set example.com as the default proxy endpoint, and default port (if it's taken, it'll search for other available port)
    const proxyServer = new ProxyServer('example.com', 5000);
    
    Promise
        .resolve()
        .then(() => 
            // stub routes matching /bar/:name.
            proxyServer.get('/bar/:name', (req, res, next) => {
                res.json({bar: req.params.name});
            })
        )
        .then(() => proxyServer.listen())
        .then(() => proxyServer.getPort())
        .then(port => console.log("listening on " + port))
        .then(() => 
            // You can also stub routes after the application started listening. The server will restart, but the port will not be changed.
            proxyServer.get('/baz/:name', (req, res, next) => {
                res.json({baz: req.params.name});
            })
        );
}

runServer();

ProxyServer has the same API for method matching as express.js.

All the methods return a promise, since some of the calls will require the server to restart. The promise will resolve after the server has started. async/await is also supported.

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago