简体   繁体   English

Nodejs Express - dynamicHelpers错误

[英]Nodejs Express - dynamicHelpers error

I'm trying to run my app with the following: 我正在尝试使用以下内容运行我的应用:

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.enable('jsonp callback');
  app.set('view engine', 'jade');
  app.set('view options', {layout : false});
  app.use(express.bodyParser());
  app.use(express.cookieParser());
  app.use(express.session({
    secret : 'abcdefg'      
  }));
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));

});

app.dynamicHelpers({
  session : function(req, res){
    return req.session;
  }
});

And getting the following error message when running node app.js 并在运行node app.js时收到以下错误消息

app.dynamicHelpers({

TypeError: Object function app(req, res){ app.handle(req, res); } has no method 'dynamicHelpers'
    at Object.<annonymous> (C:\nodeapps\nodeblox\app.js:35:5)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.Load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)
    at process.startup.processNextTick.process._tickCallBack (node.js:244:9)

Im kind of new to using nodejs but I understand that dynamicHelpers() is no longer supported with Express, I have tried re-working the code and continue to get errors all over the place. 我是使用nodejs的新手,但我知道Express不再支持dynamicHelpers(),我尝试重新编写代码并继续在整个地方出错。 What is the correct way to fix this dynamicHelpers code so that it ensures the rest of the code is still working correctly? 修复此dynamicHelpers代码的正确方法是什么,以确保其余代码仍能正常工作?

Thanks! 谢谢!

I guess that you are not using Express 2 but Express 3. There is migration guide that helps you figure out what happened. 我猜你没有使用Express 2而是Express 3.有迁移指南可以帮助你弄清楚发生了什么。 For dynamicHelpers() you need to understanding that the app.use function needs to be IN the configure part, not where it was before. 对于dynamicHelpers(),您需要了解app.use函数需要在配置部分中,而不是之前的位置。

Before 之前

app.configure();
app.dynamicHelpers({
  user: function(req, res) {
    return req.session.user;
  }
});

Now in E3 现在在E3

app.configure(function(){
  //...
  app.use(function(req, res, next){
    res.locals.user = req.session.user;
    next();
  });
  //...
});
**In Epress3.x** there seems to be having no concept of static or dynamic.
use global or local instead.

/*Global*/
app.locals({ foo: 'bar' });
//or
app.locals.foo = 'bar';

/*For single request*/
res.locals({ foo: 'bar' });
//or
res.locals.foo = 'bar';

more difference refers Here . 更多的差异是指这里

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

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