Express inspired mqtt development framework based on mosca for node.
var mors = require('mors')
var app = mors()
app.route('*', function (req, res) {
console.log('received', req.topic, req.payload);
});
app.listen(9191)
$ npm install mors
- Full features from mosca
- Express style usage
- Usable inside ANY other node.js app
- Supports node v6 ~ v8
The following settings will alter how Express behaves:
- env Environment mode, defaults to
process.env.NODE_ENV
(NODE_ENV environment variable) or "development"
Assigns setting name
to value
.
Get setting name
value.
Set setting name
to true
.
Set setting name
to false
.
Check if setting name
is enabled.
Check if setting name
is disabled.
Use the given middleware function
(with optional mount path, defaulting to "*").
var mors = require('mors');
var app= mors();
// simple logger
app.use(function(req, res, next){
console.log('PUB %s', req.topic);
next();
});
// respond
app.use(function(req, res, next){
res.publish(req.topic + '$reply', 'Hello World');
});
app.listen(9191);
The app.VERB() methods provide the routing functionality in Express, where VERB is one of the PUB/SUB verbs (such as app.post()). Multiple callbacks may be given; all are treated equally, and behave just like middleware.
This method functions just like the app.VERB() methods, however it matches all PUB/SUB verbs.
Returns an instance of a single route, which can then be used to handle PUB/SUB verbs with optional middleware. Using app.route() is a recommended approach for avoiding duplicate route names (and thus typo errors).
var app = mors();
app.route('/events')
.all(function(req, res, next) {
// runs for all PUB/SUB verbs first
// think of it as route specific middleware!
})
.subscribe(function(req, res, next) {
// on subscribe
})
.publish(function(req, res, next) {
// maybe add a new event...
})
Set authorizer
for authenticating and authorizing the clients. The authorizer
include three methods:
- authenticate(client, username, password, callback)
- authorizePublish(client, topic, payload, callback)
- authorizeSubscribe(client, topic, callback)
More information: Authentication & Authorization
Bind and listen for mutt clients on the given host and port. This method is identical to mors.Server()
witch inherits from mosca.Server
.
var mors = require('mors');
var app = mors();
app.listen(9191);
This property is an object containing properties mapped to the named route "parameters". For example, if you have the route /user/:name
, then the "name" property is available to you as req.params.name
. This object defaults to {}.
// ROUTE "/user/:name" with TOPIC "/user/ty"
req.params.name
// => "ty"
More information: routes.js
The currently matched Route containing several properties (such as the route's path string, the params, and so on).
app.get('$user/:id?', function(req, res){
console.log(req.route);
});
Example output from the previous snippet:
{
params: {
id: '1'
},
splats: [],
path: '$user/:id'
}
The topic
of the request packet.
The payload
of the request packet.
The qos
of the request packet.
The retain
of the request packet.
Set response topic
.
Set response payload
Set response qos
Set response retain
Publish a response with the message.
res.topic('$foo/bar').publish('hello');
res.topic('$foo/bar').publish('hello', cb);
Use the given middleware function
, with optional mount path
, defaulting to "*".
Middleware is like a plumbing pipe, requests start at the first middleware you define and work their way "down" the middleware stack processing for each path they match.
Currently this is treated equally, and behave just like use.
- mors-payload A mors payload parsing middleware
MIT