简体   繁体   中英

how to use the app helpers in node.js?

I want to add a script in app.js file here is the code:

 app.helpers({
      renderScriptsTags: function (all) {
        if (all != undefined) {
          return all.map(function(script) {
            return '<script src="/javascript/' + script + '"></script>';
          }).join('\n ');
        }
        else {
          return '';
        }
      }
    });

    app.dynamicHelpers({
      scripts: function(req, res) {
        return ['canvasjs.min.js'];
      }
    });

while executing its give me an error shown below:

  app.helpers({
        ^
    TypeError: Object function (req, res, next) {
        app.handle(req, res, next);
      } has no method 'helpers'
        at Object.<anonymous> (/var/www/html/nodeproject/helloworld/index.js:27:5)
        at Module._compile (module.js:456:26)
        at Object.Module._extensions..js (module.js:474:10)
        at Module.load (module.js:356:32)
        at Function.Module._load (module.js:312:12)
        at Function.Module.runMain (module.js:497:10)
        at startup (node.js:119:16)
        at node.js:902:3

As your error is showing app does not have helper funtion. Memeber functions of app are listed here http://expressjs.com/en/api.html

if you want to use helpers you will have to create them explicity like

//helper.js
module.export = function(){
// Your logic here
};

//usage
var helper = require('./relative/path/to/helper');
var getValueFromHelper =  helper();

hope it helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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