我正在寻找一种方法来访问发送给控制器的“后”过滤器中的请求者的JSON。
var locomotive = require('locomotive');
var myController = new locomotive.Controller();
myController.after('myAction', function(next) {
var response = {}; //I want to access the JSON being sent back in myAction: {'hello':'world'}
console.log(response); //this should log "{'hello':'world'}"
next();
});
myController.myAction = function myAction() {
this.res.json({'hello':'world'});
}
module.exports = myController;如果有人有办法这么做的话,我会非常感激的。
发布于 2014-05-28 14:18:56
我找到了一个“黑客”解决方案..。它不是最干净的,需要更改“node_modules”中的express node_modules文件中的代码.
如果有人有一个更好的选项,您可以访问在控制器操作(或控制器过滤器)中响应请求而发送的json,我将非常感激。
谢谢。
在~/node_modules/locomotive/node_modules/express/lib/response.js文件中,我修改了"res.json“函数(对于我来说是第174行),以便在body变量声明(传递给send函数)之后包含下面的行。
this.responseJSON = body;这允许您在控制器的后筛选器中访问this.responseJSON,如下所示:
myController.after('myAction', function(next) {
**var response = this.res.responseJSON; //ACCESS RESPONSE JSON HERE!!!!!**
console.log(response); //Now logs "{'hello':'world'}"
next();
});就像我说的,不是最优雅的,而是在紧要关头完成工作。任何更优雅的解决方案欢迎..。
发布于 2014-05-14 02:43:29
在您的主要操作中,将json分配给这个对象(res是保留的):
myController.myAction = function myAction() {
this.model = {'hello':'world'};
this.res.json(this.model);
}然后您可以在后续筛选器中访问它:
myController.after('myAction', function(next) {
var model = this.model;
console.log(model);
next();
});https://stackoverflow.com/questions/23552777
复制相似问题