Blackbird is a reverse proxy, with built-in node cluster and load balancing.
To install Blackbird
you can easily run below command:
npm i @namatery/blackbird
First of all, create a proxy server like below:
const proxy = new ReversProxy({
http: {
port: 3000,
},
});
NOTE:
http.port
is required.
To register a domain, do like this:
proxy.register({
source: 'http://localhost:3000/',
target: 'http://test.com/',
opts: {
redirect: false,
ws: false,
},
});
source
[required]: The url that client make a request to it.target
[required]: The url that if client makes a reqeust tosource
, Blackbird redirects the request to it.redirect
[optional]: If settrue
, Blackbird automatically redirects to HTTPS.ws
[optional]: If settrue
, Blackbird allows clients to make socket connection.
- You can set several
target
for a singlesource
. - Before set
redirect
astrue
, make sure you have configured proxy for HTTPS requests. - For load balancing, Blackbird uses round-robin algorithm.
To enable HTTPS, you can pass creditions like below:
const proxy = new ReversProxy({
http: {
port: 3000,
},
https: {
port: 9000,
cert: 'path/to/cert.pem',
key: 'path/to/key.pem',
ca: 'path/to/ca.pem',
},
});
NOTE 1:
port
is required.
NOTE 2:
cert
,key
andca
can be buffers. (ca
is optional)
In the above example, the creditoins that we set wil use for all registerd domain, but you can also attach the creditions for a specifyed domain.
proxy.attachSSL({
hostname: 'localhost:3000',
cert: 'path/to/cert.pem',
key: 'path/to/key.pem',
ca: 'path/to/ca.pem',
});
NOTE 1:
hostname
is the hostname that we registerd assource
above as you saw.
NOTE 2: You can't attach several creditions for a single
hostname
.
To start proxy, you have two options. You can either run it in the normal way like below:
proxy.start();
Or run it in the cluster mode like below:
proxy.startCluster(3);
In this case Blackbird will run three instances of our proxy. To learn more about cluster
in nodejs go to node-cluster.
Yes! Blackbird uses Express Cors package to handle this concept and you can enable it like below:
const proxy = new ReversProxy({
http: {
port: 3000,
},
cors: {
origin: 'http://localhost:3000/'
}
});
NOTE 1: All the options is similar with Express Cors package.
- Writing Unite test for Table repository.
- Writing Unite test for SSL repository.
- CORS error on redirecting from http to https.
- CORS error on websocket communication.
- Writing Unite test for proxy class.