2017-07-07 56 views
0

我正在爲我的節點服務器編寫一些代碼,並且本地一切正常。但是,當我部署它,我得到臭名昭着的「無法GET /」404錯誤。我寫的所有其他路線都已成功(本地和雲端)。節點快遞服務器無法獲取「/」

我已經包含下面的代碼。我檢查了我的環境變量,並將它們全部對齊。如果你們有任何關於這個原因的理論,我們將非常感激。

//Use this route to make the entire app secure. This forces login for any path in the entire app. 
 
    app.use('/', passport.authenticate('main', { 
 
    noredirect: false //Don't redirect a user to the authentication page, just show an error 
 
    }), 
 
    express.static(path.join(__dirname, '../public')) 
 
);

回答

0

爲了澄清@ aprouja1

// You usually want to declare statics separately for code organisation 
app.use(express.static(path.join(__dirname, '../public'))) 

app.use('/', 
    passport.authenticate('main', { 
    noredirect: false 
    }), 
    function(req, res) { 
    res.sendFile('index.html') 
    } 
); 

因爲除了任何複雜的應用程序的索引頁面,它可能會更好使用渲染引擎,例如哈巴狗。

app.set('views', __dirname + '/views'); // Not required. Set by default. Being explicit. 
app.engine('pug', require('pug').__express); // Not required. Automatically loaded by express. Again, being explicit. 
app.set('view engine', 'pug'); // Required, see: https://expressjs.com/en/guide/using-template-engines.html 

app.use('/', 
    passport.authenticate('main', { 
    noredirect: false 
    }), 
    function(req, res) { 
    res.render('index') // Renders `/views/index.pug` 
    } 
); 
0

它看起來並不像你所服務的用戶的任何文件,當他們瀏覽到這條路線。你有一個index.html或其他文件?如果是這樣,你可以用這個你app.use函數中:

res.sendFile('index.html') 
相關問題