api-proxy.js
v0.1.0
Published
api-proxy.js is an easy-to-use Node.js service proxy that serves as a communication bridge between internal and external networks without affecting business logic.
Downloads
51
Readme
api-proxy.js
api-proxy.js is an easy-to-use Node.js service proxy that serves as a communication bridge between internal and external networks without affecting business logic.
Introduction
api-proxy.js is a proxy service used to forward external API requests to the internal network and return the response. It relies on socket.io, socket.io-client, and express.
Service Overview
| Service | Name | Deployment Location | Function | | ------- | ------------------------ | ------------------- | ------------------------------------------------------------------------------- | | proxy | Proxy Service | External Server | Forwards external API requests to the internal network and returns the response | | server | Internal Request Service | Internal Server | Used to request internal API services | | client | External Access Service | Request Service | Receiving end of external access API services |
Installation
Install using npm:
npm install socket.io socket.io-client express api-proxy.jsConfiguration
{
"clientToken": "client", // client, proxy
"proxyHost": "http://localhost:8000", // client, server
"secretKey": "123456", // client, server, proxy
"serverToken": "server" // server, proxy
}Usage
proxy
import { createProxy } from "api-proxy.js"; import express from "express"; export default function (config = {}) { const { clientToken, proxyHost, secretKey } = config; const app = express(); app.use( createProxy({ clientToken, proxyHost, secretKey, }) ); app.listen(8000, () => { console.log(`proxy is running at http://localhost:8000`); }); }server
import { createServer } from "api-proxy.js"; export default function (config = {}) { const { serverToken, proxyHost, secretKey } = config; createServer( { serverToken, proxyHost, secretKey, }, async (actions) => { // actions from client // do something ... return { // response }; } ); }client
import { createClient } from "api-proxy.js"; import express from "express"; export default function (config = {}) { const { clientToken, proxyHost, secretKey } = config; const app = express(); app.use( createClient({ clientToken, proxyHost, secretKey, actionHandler: (req) => { // do something ... return { // actions to client }; }, responseHandler: (res, result) => { // do something ... res.json({ // ... }); }, }) ); app.listen(8008, () => { console.log("client is running at http://localhost:8008"); }); }
