【问题标题】:How to include js file with no error?如何包含没有错误的js文件?
【发布时间】:2017-08-09 22:20:38
【问题描述】:

我想在 nodejs 中分离我的路由文件。我使用 restify 框架,这是我的代码的一部分:

app.routes((app)=>{
  require(__dirname + '/../routes/web.js')
})

路由/web.js:

app.get('/', function (req, res, next) {
    console.log("object");
})

运行程序时出现此错误:

应用未定义

我该如何解决这个问题?

在 web.js 中,appundefined,而我将其发送到函数中。

【问题讨论】:

    标签: javascript node.js restify


    【解决方案1】:

    在主文件中

    require(__dirname + '/../routes/web.js')(app)
    

    路由/web.js:

    module.exports = function(app){
        app.get('/', function (req, res, next) {
            res.send("object");
        });
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-10
      • 1970-01-01
      • 2020-03-14
      • 1970-01-01
      相关资源
      最近更新 更多