简体   繁体   English

node.js express-resource自动加载不起作用

[英]node.js express-resource auto-load is not working

Has anyone managed to get the auto-load functionality of express-resource to work? 有没有人设法使express-resource的自动加载功能起作用? I cannot get it to fire, but according to the documentation it should work: express-resource 我无法启动它,但是根据文档,它应该可以工作: express-resource

I have tried getting it to work in both my own code and the example code in express-resource provided here . 我已经尝试使其在我自己的代码和这里提供的 express-resource中的示例代码中都可以使用 I am simply adding a "console.log" statement into the forum resource's "load" function, however it seems to never be called. 我只是在论坛资源的“加载”函数中添加“ console.log”语句,但是似乎从未调用过它。 Code is as follows: 代码如下:

exports.load = function(id, fn){
  console.log('auto-loader');
  process.nextTick(function(){
    console.log('auto-loader in the callback');
    fn(null, { title: 'Ferrets' });
  });
};

Then in the "controller.js" the code is exactly as in the example here : 然后,在“ controller.js”中,代码​​与此处示例完全相同:

var forums = app.resource('forums', require('./controllers/forum'));

The reason I would like to get this working is that I want to have a blanket function that loads some data for a particular set of routes, as opposed to adding it into each route individually as middleware. 我之所以想开始这项工作,是因为我想要一个覆盖函数,该函数为一组特定的路由加载一些数据,而不是将其作为中间件分别添加到每个路由中。 Eg I have "listings" on my site, and I want a function that retrieves the top 10 recent listings. 例如,我的网站上有“列表”,我想要一个函数来检索最近的前10个列表。

Any ideas would be greatly appreciated I spent much of last night puzzling and concluding that the code may not work at all. 任何想法都将不胜感激,我昨晚花了很多时间迷惑并断定该代码可能根本无法工作。 I sincerely hope I'm wrong and hope to stand corrected by one of you smart cookies, because I'd really like it if I did not have to pick a different routing module! 我衷心希望我错了,希望能被你们其中的一个智能cookie纠正,因为如果我不必选择其他路由模块,我将非常喜欢它!

Alternatively if you have a routing module that works with express that you know has functioning auto-load capability then please also let me know? 另外,如果您有一个与Express一起工作的路由模块,并且您知道它具有自动加载功能,那么也请告知我吗?

Alright, in case anyone else runs into this - I have found that I'm incorrect and it does actually fire, but only sometimes!! 好吧,以防万一其他人遇到这个问题-我发现我不正确, 它确实会触发,但只是有时!

The problem is that it does not fire on all routes. 问题在于它不会在所有路线上触发。 So far I have tested index, new, show, and edit. 到目前为止,我已经测试了索引,新建,显示和编辑。 It fires for show and edit but not for index or new. 它会触发以进行显示和编辑,但不会触发索引或新建。

I am not sure if this is intentional or not and would love it if someone could shed some light on why it behaves like this please ? 我不确定这是否是故意的,如果有人可以阐明为什么会这样,我会喜欢吗?

I have also left a comment on a very similar question on the author's github site: load function doesn't add to req object on index route 我还对作者的github网站上的一个非常类似的问题发表了评论: 加载功能未添加到索引路由上的req对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM