2016-11-07 107 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