Fix API errors, add slight simplifications
This commit is contained in:
parent
b5907ccd36
commit
7109a9d303
19
lib/api.js
19
lib/api.js
|
@ -14,16 +14,17 @@ var fs = require("fs"),
|
|||
var express = require("express"),
|
||||
bodyParser = require("body-parser");
|
||||
|
||||
var Logger = require("./logger");
|
||||
var Logger = require("./logger"),
|
||||
_ = require("./lodash");
|
||||
|
||||
var API = module.exports = function API(opts) {
|
||||
if (opts == null) {
|
||||
opts = {};
|
||||
}
|
||||
|
||||
for (var d in this.defaults) {
|
||||
this[d] = opts.hasOwnProperty(d) ? opts[d] : this.defaults[d];
|
||||
}
|
||||
_.forEach(this.defaults, function(def, name) {
|
||||
this[name] = _.has(opts, name) ? opts[name] : def;
|
||||
}, this);
|
||||
|
||||
this.createServer();
|
||||
|
||||
|
@ -46,12 +47,7 @@ var API = module.exports = function API(opts) {
|
|||
|
||||
// extracts command params from request
|
||||
this.express.use(function(req, res, next) {
|
||||
req.commandParams = [];
|
||||
|
||||
for (var p in req.body) {
|
||||
req.commandParams.push(req.body[p]);
|
||||
}
|
||||
|
||||
req.commandParams = _.values(req.body);
|
||||
return next();
|
||||
});
|
||||
|
||||
|
@ -59,8 +55,9 @@ var API = module.exports = function API(opts) {
|
|||
this.express.use("/api", require("./api/routes"));
|
||||
|
||||
// error handling
|
||||
this.express.use(function(err, req, res) {
|
||||
this.express.use(function(err, req, res, next) {
|
||||
res.status(500).json({ error: err.message || "An error occured."});
|
||||
next();
|
||||
});
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue