moleculer-web
The moleculer-web is the official API gateway service for Moleculer framework. Use it to publish your services.
Features
- support HTTP & HTTPS
- serve static files
- multiple routes
- support Connect-like middlewares in global-level, route-level and alias-level.
- alias names (with named parameters & REST routes)
- whitelist
- multiple body parsers (json, urlencoded)
- CORS headers
- Rate limiter
- before & after call hooks
- Buffer & Stream handling
- middleware mode (use as a middleware with Express)
- support authorization
Install
npm i moleculer-web |
Usage
Run with default settings
This example uses API Gateway service with default settings.
You can access all services (including internal $node.
) via http://localhost:3000/
let { ServiceBroker } = require("moleculer"); |
Example URLs:
Call
test.hello
action:http://localhost:3000/test/hello
Call
math.add
action with params:http://localhost:3000/math/add?a=25&b=13
Get health info of node:
http://localhost:3000/~node/health
List all actions:
http://localhost:3000/~node/actions
Whitelist
If you don’t want to publish all actions, you can filter them with whitelist option.
You can use match strings or regexp in list.
broker.createService({ |
Aliases
You can use alias names instead of action names. You can also specify the method. Otherwise it will handle every method types.
Using named parameters in aliases is possible. Named parameters are defined by prefixing a colon to the parameter name (:name
).
broker.createService({ |
The named parameter is handled with path-to-regexp module. Therefore you can use optional and repeated parameters as well.
You can also create RESTful APIs.
broker.createService({ |
For REST routes you can also use this simple shorthand alias:
broker.createService({ |
To use this shorthand alias you need to create a service which has
list
,get
,create
,update
andremove
actions.
You can make use of custom functions within the declaration of aliases.
broker.createService({ |
You have some internal pointer in
req
&res
to some important instances:
req.$ctx
are pointed to request context.req.$service
&res.$service
is pointed to the service instance.req.$route
&res.$route
is pointed to the route definition.req.$params
is pointed to the resolved parameters (from query string & post body)req.$alias
is pointed to the alias definition.req.$endpoint
is pointed to the resolved action endpoint. It contains action and nodeID.E.g., if you would like to access the broker, use
req.$service.broker
path.
Mapping policy
The route
has a mappingPolicy
property to handle routes without aliases.
Available options:
all
- enable to request all routes with or without aliases (default)restrict
- enable to request only the routes with aliases.
broker.createService({ |
You can’t request the /math.add
or /math/add
URLs, only POST /add
.
Middlewares
It supports Connect-like middlewares in global-level, route-level & alias-level. Signature: function(req, res, next) {...}
.
Example
broker.createService({ |
Serve static files
It serves assets with the serve-static module like ExpressJS.
broker.createService({ |
Calling options
The route
has a callOptions
property which is passed to broker.call
. So you can set timeout
, retryCount
or fallbackResponse
options for routes. Read more about calling options
broker.createService({ |
Multiple routes
You can create multiple routes with different prefix, whitelist, alias, calling options & authorization.
broker.createService({ |
Authorization
You can implement authorization. For this you have to do 2 things.
- Set
authorization: true
in your routes - Define the
authorize
method of service.
Example authorization
const E = require("moleculer-web").Errors; |
You can find a more detailed role-based JWT authorization example in full example.
Route hooks
The route
has before & after call hooks. You can use it to set ctx.meta
, access req.headers
or modify the response data
.
broker.createService({ |
Error handlers
You can add route-level & global-level custom error handlers.
In handlers, you must call the
res.end
. Otherwise, the request is unhandled.
broker.createService({ |
CORS headers
You can use CORS headers in Moleculer-Web service.
Usage
const svc = broker.createService({ |
Rate limiter
The Moleculer-Web has a built-in rate limiter with a memory store.
Usage
const svc = broker.createService({ |
Custom Store example
class CustomStore { |
ExpressJS middleware usage
You can use Moleculer-Web as a middleware in an ExpressJS application.
Usage
const svc = broker.createService({ |
Full service settings
List of all settings of Moleculer Web service:
settings: { |
Examples
-
- simple gateway with default settings.
-
- open HTTPS server
- whitelist handling
-
- serve static files from the
assets
folder - whitelist
- aliases
- multiple body-parsers
- serve static files from the
-
- simple authorization demo
- set the authorized user to
Context.meta
-
- simple server with RESTful aliases
- example
posts
service with CRUD actions
-
- webserver with Express
- use moleculer-web as a middleware
-
- start socket.io websocket server
- call action and send back the response via websocket
- send Moleculer events to the browser via websocket
-
- SSL
- static files
- middlewares
- multiple routes with different roles
- role-based authorization with JWT
- whitelist
- aliases with named params
- multiple body-parsers
- before & after hooks
- metrics, statistics & validation from Moleculer
- custom error handlers
-
- Webpack development environment for client-side developing
- webpack config file
- compression
- static file serving
-
- Webpack+Vue development environment for VueJS client developing
- webpack config file
- Hot-replacement
- Babel, SASS, SCSS, Vue SFC