From 7109a9d303dc8421ccc74a972607cb41079f5272 Mon Sep 17 00:00:00 2001 From: Andrew Stewart Date: Wed, 17 Dec 2014 16:51:48 -0800 Subject: [PATCH] Fix API errors, add slight simplifications --- lib/api.js | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/lib/api.js b/lib/api.js index b8d3b6d..c9df43e 100644 --- a/lib/api.js +++ b/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(); }); };