2017-07-07 52 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')