meteor-wapi v0.3.1
meteor-wapi
Node module to allow CQRS-ing with Meteor.
Install
npm i --save meteor-wapi
Example
var express = require("express");
var MongoClient = require("mongodb").MongoClient;
var MW = require("meteor-wapi");
var mongoUrl = process.env.MONGO_URL || "mongodb://localhost:3001/meteor";
MongoClient.connect(mongoUrl, function (err, db) {
var mw = new MW(db);
var optionalContext = {
prefix: "echo: "
};
mw.methods({
echo: function (string) {
return this.prefix + string;
}
}, optionalContext);
var app = express()
.use("/call", mw.getRouter())
.listen(process.env.PORT || 4000);
});API
new MW(db)
Creates a new MW instance.
Arguments
dbMongoClient connection required: a mongodb connection, as returned (via callback) by theMongoClient.connectmethod.
Returns
An MW instance.
.methods(methodsMap, optionalContext)
Registers one or more remote methods (mimicking Meteor's API). The methods will
be invoked when the client POSTs a DDP method message to the server.
Methods can either:
- throw an error
- return a value
- return a promise
If the method returns a value (or an eventually fulfilled promise), the client will receive the returned (or resolved) value in the DDP response message.
If the method throws (or returns an eventually rejected promise), the client
will receive the error in the DDP response message. If the error is an instance
of MW.Error, its code and message will be used for the reply. Otherwise a
generic 500 INTERNAL SERVER ERROR error will be used.
Arguments
methodsMapstring-function dictionary required: a dictionary of functions (just like in Meteor).optionalContextobject optional: an optional object which will be mixed in with the execution context of the method (thethisvalue).
Returns
Nothing.
.getRouter()
Returns an express router which listens for POSTed DDP method messages,
authenticates them and runs the appropriate methods.
Arguments
None.
Returns
An express router (to be used as argument to an express app use method).
Test
After cloning the repository and installing dependecies with npm install:
- to run unit tests:
npm run unit-tests - to run integration tests:
npm run integration-tests(you need a running local mongo listening on port 27017)