moleculer-web
Advanced tools
Changelog
0.6.2 (2018-01-15)
preValidate
setting. Default to true
in order to backward compatibility.
broker.createService({
mixins: [ApiService],
settings: {
// Disable pre-validation at action calls
preValidate: false
}
})
<a name="0.6.1"></a>
Changelog
0.6.0 (2018-01-04)
The route
first argument is removed. The new signature of function is function(req, res) {}
. To access to route use the req.$route
property.
However you can use an array of Function
for aliases. With it you can call middlewares. In this case the third argument is next
. I.e.: function(req, res, next) {}
.
UTF-8
for application/json
responses.logRequestParams
setting to log the request parameters. Use log level value i.e. "debug"
, "info"
or null
to disable.logResponseData
setting to log the response data. Use log level value i.e. "debug"
, "info"
or null
to disable.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
.Support middlewares in global, routes & aliases.
broker.createService({
mixins: [ApiService],
settings: {
// Global middlewares. Applied to all routes.
use: [
cookieParser(),
helmet()
],
routes: [
{
path: "/",
// Route-level middlewares.
use: [
compression(),
passport.initialize(),
passport.session(),
serveStatic(path.join(__dirname, "public"))
],
aliases: {
"GET /secret": [
// Alias-level middlewares.
auth.isAuthenticated(),
auth.hasRole("admin"),
"top.secret" // Call the `top.secret` action
]
}
}
]
}
});
It supports custom response headers to define in action definition.
module.exports = {
name: "export",
actions: {
downloadCSV:
responseHeaders: {
"Content-Disposition": "attachment; filename=\"data.csv\"",
"Content-Type": "text/csv"
},
handler() {
return "...";
}
}
}
}
You can add route & global custom error handlers.
broker.createService({
mixins: [ApiService],
settings: {
routes: [{
path: "/api",
// Route error handler
onError(req, res, err) {
res.setHeader("Content-Type", "application/json; charset=utf-8");
res.writeHead(500);
res.end(JSON.stringify(err));
}
}],
// Global error handler
onError(req, res, err) {
res.setHeader("Content-Type", "text/plain");
res.writeHead(501);
res.end("Global error: " + err.message);
}
}
}
<a name="0.5.2"></a>