@optimum-partners/node-api-gateway
v1.0.5
Published
## installation
Downloads
310
Readme
node-api-gateway
installation
npm i @optimum-partners/node-api-gateway
usage
import { gateway } from '@optimum-partners/node-api-gateway';
...
gateway(config);
or
import gateway from '@optimum-partners/node-api-gateway';
...
gateway(config);
config
| key | default |
| ------------------- | ----------- |
| cors
| '*'
|
| port
| 3000
|
| maxFileSize
| '2gb'
|
| cluster
| ''
|
| jwt.secret
| |
| jwt.algorithms
| ['HS256']
|
| securedBodyTokens
| [...]
|
| services
| []
|
cors
: CORS settingsport
: server portmaxFileSize
: max allowed file size for uploadcluster
: environment identifierjwt
: jwt settingssecuredBodyTokens
: secure sensitive data from logs (when key is found in the request body it will be replaced with '*')services
: microservices config
required config
jwt.token
must be provided to the library
default config
{
port: 3000,
maxFileSize: '2gb',
cluster: '',
cors: '*',
jwt: {
algorithms: ['HS256'],
},
securedBodyTokens: [
{ key: 'number', regex: /.(?=.{4,}$)/g },
{ key: 'cvc', regex: /[0-9]/g },
{ key: 'password', regex: /.*/ },
],
services: [],
}
services config
{
"name": SERVICE_NAME,
"server": SERVICE_SERVER_HOST,
"port": SERVICE_PORT,
"prefix": SERVICE_PREFIX,
"requestPaths": [
{ "path": WILD_CARD_ROUTE, "publicRoutes": [PUBLIC_ROUTES_LIST] },
]
}
name
: service name, used to identify the servicesserver
: the hostname of the hosting serverport
: the hosting server portprefix
: the service prefix, used in/healthcheck/all
endpointrequestPaths
: routes mappingpath
: routepublicRoutes
: list of string/regex to exclude public routes from JWT validation
multiple environments support
the server name can include
{CLUSTER}
token that will be replaced with thecluster
config key provided in the config object, this will allow to target an environment-based cluster
sample
{
name: 'service1',
server: 'service1-{CLUSTER}-svc',
port: 8080,
prefix: 'service1',
requestPaths: [
{ path: '/service1/healthcheck', publicRoutes: ['/service1/healthcheck'] },
{ path: '/api/public_routes/*', publicRoutes: [/\/api\/public_routes\/*/i] },
{ path: '/api/private_routes/*', publicRoutes: [] },
],
},
{
name: 'service2',
server: 'service2-{CLUSTER}-svc',
port: 8080,
prefix: 'service2',
requestPaths: [
{ path: '/service2/healthcheck', publicRoutes: ['/service2/healthcheck'] },
{ path: '/api/mix_routes/*', publicRoutes: [/\/api\/mix_routes\/public_routes\/*/i] },
],
},
Keep in mind !
The order of the routes in the services config matters
sample
{
name: 'service1',
server: 'service1-{CLUSTER}-svc',
port: 8080,
prefix: 'service1',
requestPaths: [
{ path: '/api/entity/:id/specific/*', publicRoutes: [] },
],
},
{
name: 'service2',
server: 'service2-{CLUSTER}-svc',
port: 8080,
prefix: 'service2',
requestPaths: [
{ path: '/api/entity/*', publicRoutes: [] },
],
},
in the above example route /api/entity/:id/specific/*
will be forwarded to "service1" and all other routes related to /api/entity
will be forwarded to "service2"