2016-11-07 109 views
1

我想从app.js文件中分离我的路线。需要参数

登录路线需要一个Firebase实例。

路线/ auth.js

var express = require('express'); 
var router = express.Router(); 

module.exports = function(firebase) { 
    ... 
} 

module.exports = router; 

app.js

var firebase = require('firebase'); 

var config = { 
    ... 
} 

firebase.initializeApp(config); 

var auth = require('./routes/auth')(firebase) 

app.use('/admin', auth) 

当我启动服务器,它给了我一个TypeError: Cannot read property 'indexOf' of undefined错误...

它指向app.js中的require语句:

var auth = require('./routes/auth')(firebase)


编辑:

当我尝试访问/auth它给了我一个不能得到/ AUTH错误..

app.js

const PORT = 8081 

... 

var auth = require('./routes/auth')(firebase) 

app.use('/auth', auth) 

app.listen(PORT, function() { 
    console.log(util.format('Example app listening on port %d!', PORT)) 
}) 

路/ auth.js

var express = require('express'); 
var router = express.Router(); 

module.exports = function(firebase) { 
    router.get('/auth', function(req, res) { 
    res.send('hi') 
    }) 

    return router 
} 

的URL我尝试访问http://localhost:8081/auth

+0

是,错误指向需要声明 – yooouuri

+0

对不起,我更新的问题! – yooouuri

+2

你的auth.js,有2个出口..所以你最后的出口将赢。我认为你的后面更像 - >'module.exports = function(firebase){return router; }' – Keith

回答

2

对于第一个问题..

你auth.js,有2个出口。所以你最后的出口会赢。我认为你的后面更像 - > module.exports = function(firebase){return router; }

第二个问题是你使用app.use(url,obj)..你提供的url将成为你的中间件的根节点。所以当你做了router.get(url,callback)时,什么是随后发生的事情就是网址将成为这里/aut/auth

2个选项,

  1. 不提供根,例如。 app.use(auth)
  2. 从获取删除的网址,因为它已经从app.use设置,所以router.get('/', callback)
+0

谢谢你,你是最好的! – yooouuri