Created
September 25, 2015 17:40
-
-
Save noahjs/7f2373cdf502431c6e7d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Router.prototype.addRoute = function(verb, path, controller, method, params) { | |
// // Set default params | |
// if (!params) { | |
// params = function() { | |
// return {}; | |
// }; | |
// } | |
this.controllerKeys[path] = controller; | |
this.router[verb](path, function(req, res, next) { | |
// Get controller | |
var controller = this.getController(this.controllerKeys[path], req, res); | |
var appController = this; | |
// Populate params | |
//controller.params = params(req); | |
// Add client id and querystring to params | |
controller.body = req.body; | |
controller.params = req.params; | |
controller.querystring = url.parse(req.url, true).query; | |
controller.session = req.session; | |
logger.silly( 'Calling controller-method: ' + method ); | |
// Handle next, err | |
controller[method](function(err, arg1, arg2) { | |
if( appController.outputHandler == 'json' ){ | |
var template = false; | |
var data = arg1; | |
}else{ | |
var template = arg1; | |
var data = arg2; | |
} | |
// init if empty | |
if( !data ) data = {}; | |
if (err && appController.outputHandler != 'json' && controller.querystring['format'] != 'json' && req.accepts('html') =="html" ) { | |
var responseFormat = 'html'; | |
}else if (template && template !="json" && appController.outputHandler != 'json' && controller.querystring['format'] != 'json' && req.accepts('html') =="html" ) { | |
var responseFormat = 'html'; | |
}else{ | |
var responseFormat = 'json'; | |
} | |
// Is there an Override on DATA ? | |
if( responseFormat == 'html' && appController.dataOverride !== false ){ | |
return appController.dataOverride(req, res, data, function(newdata){ | |
return complete(newdata) | |
}); | |
}else{ | |
return complete(data) | |
} | |
// and finish up/ | |
function complete(){ | |
// REFRESH the live Session | |
if( req.authenticator ){ | |
req.authenticator.refresh(function(err, sessionKey){ | |
res.cookie('auth_cookie', sessionKey, { maxAge: 864000000, httpOnly: true }); | |
}); | |
} | |
if (err) { | |
if( err.message ) | |
logger.error(err.message); | |
if( typeof err == "string"){ | |
return res.status(500).json({ | |
error: { | |
message: err, | |
type: "general" | |
} | |
}); | |
}else if ( responseFormat == "html" ) { | |
return res.render(appController.viewsPath + "/" + 'error', err); | |
}else { | |
var code = err.code ? err.code : 500; | |
var msg = err.message ? err.message : "Error"; | |
var type = err.type ? err.type : "General"; | |
return res.status( code ).json({ | |
error: { | |
message: msg, | |
type: msg | |
} | |
}); | |
} | |
//next(err); | |
} else { | |
if ( template && responseFormat == "html" ) { | |
return res.render(appController.viewsPath + "/" + template, data); | |
} else { | |
// Process JSON API response | |
if (data == null) { | |
data: {} | |
}else if (data.pretty) { | |
data = data.pretty(); | |
}else if (data instanceof Array) { | |
for (var i = 0; i < data.length; i++) { | |
if (data[i].pretty) { | |
data[i] = data[i].pretty(); | |
} | |
} | |
} | |
// respond | |
return res.send({ | |
data: data | |
}); | |
} | |
} | |
} | |
}); | |
}.bind(this)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment