If you wish for all promises to be handled as an API response, simply call standardProcessing on the response variable, passing in the promise or function that is to be handled.
router.get('/', function(req, res){
var modelCall = new Promise(function(resolve, reject){
return resolve(new AdapterResult(200, { data: "some data" }));
});
res.standardProcessing(modelCall);
});
When a promise resolves, it is treated as success with a default code of 200.
When a promise rejects, it is treated as a failure with a default code of 500.
If simple data is returned from the promise, it will be returned with the default status code
var modelCall = new Promise(function(resolve, reject){
return resolve("Your name is Jim");
});
res.standardProcessing(modelCall);
var modelCall = new Promise(function(resolve, reject){
return resolve({ name: "Jim" });
});
res.standardProcessing(modelCall);
To return a specific status code, use the AdapterResult class, passing in the status code and object to return.
var modelCall = new Promise(function(resolve, reject){
return resolve(new AdapterResult(202, { data: "some data" }));
});
res.standardProcessing(modelCall);
To add global error handling to the app, add globalHandler to a use statement at the end of all server setup.
var app = require('express')();
...
app.use(require('express-handling').globalHandler);