[英]Node Express Server Cannot 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')) );
當用戶導航到該路由時,似乎沒有向用戶提供任何文件。 您是否有index.html或其他文件? 如果是這樣,您可以在app.use函數中使用它:
res.sendFile('index.html')
澄清@ 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')
}
);
對於除具有任何復雜性的應用程序之外的索引頁,最好使用渲染引擎,例如pug。
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`
}
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.