[英]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.