Simple proxy for your microservices based on
servie
andpath-to-regexp
.
npm install microservice-proxy --save
Create the configuration:
{
"rules": [
{
"path": "/login",
"methods": ["get"],
"url": "http://localhost:3001"
},
{
"url": "http://localhost:3000"
}
]
}
Start the proxy:
microservice-proxy -c config.json -p 9000
Or programmatically:
import { proxy } from "microservice-proxy";
import { createHandler } from "servie-http";
import { createServer } from "http";
const app = createHandler(
proxy([
{
url: "http://example.com"
}
])
);
createServer(app).listen(3000);
Originally, this module was going to manage processes themselves and dynamically assign port numbers. Unfortunately, it's a little complex to manage the dependencies between the process ports (leaving this for another day). I found micro-proxy
which is designed to do the same sort of thing, but it had a couple of issues - path matching and no HTTP/2 requests. I wrote this using ServieJS as an interesting case-study of how decoupling the request and response can lead to interesting mixups such as this (e.g. this could be deployed to AWS Lambda).
This project is written using TypeScript and publishes the definitions directly to NPM.
Apache 2.0