tenancy-moleculer-web
v0.10.5
Published
Official API Gateway service for Moleculer framework
Downloads
340
Maintainers
Readme
Official API Gateway for Moleculer framework
The moleculer-web
is the official API gateway service for Moleculer. Use it to publish your services.
Features
- support HTTP & HTTPS
- serve static files
- multiple routes
- support global, route, alias middlewares
- support file uploading
- alias names (with named parameters & REST shorthand)
- whitelist
- multiple body parsers (json, urlencoded)
- CORS headers
- ETags
- HTTP2
- Rate limiter
- before & after call hooks
- Buffer & Stream handling
- middleware mode (use as a middleware in ExpressJS Application)
- support authorization
Install
npm install tenancy-moleculer-web --save
Usage
Run with default settings
This example uses API Gateway service with default settings.
You can access to all services (including internal $node.
) via http://localhost:3000/
let { ServiceBroker } = require("moleculer");
let ApiService = require("tenancy-moleculer-web");
let broker = new ServiceBroker({ logger: console });
// Create a service
broker.createService({
name: "test",
actions: {
hello() {
return "Hello API Gateway!"
}
}
});
// Load API Gateway
broker.createService(ApiService);
// Start server
broker.start();
Test URLs:
Call
test.hello
action:http://localhost:3000/test/hello
Get health info of node:
http://localhost:3000/~node/health
List all actions:
http://localhost:3000/~node/actions
Documentation
Please read our documentation on Moleculer site
Support Multiple Tenancy
let { ServiceBroker } = require("moleculer");
let ApiMixin = require("tenancy-moleculer-web");
let broker = new ServiceBroker({ logger: console });
broker.createService({
name: "test",
mixins: [ApiMixin]
settings: {
port: Number(process.env.PORT || 3000),
routes: [
{
{
path: "/api",
authentication: false,
tenancy: true, // Check Tenant existed or not?
whitelist: ["user.*"],
aliases: {
"GET /users": "user.list",
},
},
}
]
},
actions: {
hello() {
return "Hello API Gateway!"
}
},
methods: {
tenancy(ctx, route, req) {
const tenantId = req.headers["x-tenant"]
if (!tenant) {
throw new Errors.MoleculerClientError(
ERRORS.Unauthorization.message,
ERRORS.Unauthorization.code
);
}
const tenant = { id: tenantId, name: 'Sample Tenant' }
// Return info will be mapped into ctx.meta.tenant
return tenant
}
}
});
Test
$ npm test
In development with watching
$ npm run ci
License
Moleculer-web is available under the MIT license.
Contact
Copyright (c) 2016-2021 MoleculerJS