我正在設置我的第一個節點項目。所有的頁面都很好地工作,直到我開始移動一些東西。我在index.js
中爲不同頁面完成了所有路由。我已經改變了這一點,並在我的rotes文件夾中創建了一個login.js
文件來分解一些邏輯。之前,所有的網址都正常工作並正確顯示網頁。發佈重構後,我一直在404頁面找不到所有登錄路由的錯誤。某些路由轉到404錯誤頁面快遞
app.js:
var express = require('express');
var exphbs = require('express-handlebars');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var passport = require('passport');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var login = require('./routes/login');
//Using firebase initialized in config file.
var database = require('./config/firebase');
var app = express();
///Setting stuff up here .....
app.use('/', routes);
app.use('/login', login);
app.use(function(req,res){
res.status(404);
res.render('404');
});
module.exports = app;
index.js:
var express = require('express');
var router = express.Router();
var csrf = require('csurf');
var csrfProtection = csrf();
var firebase = require('firebase');
var login = require('./login');
//tell express: All routes should be protected by csrf protection.
router.use(csrfProtection);
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('home', { title: 'Express' });
});
module.exports = router;
login.js:
var express = require('express');
var router = express.Router();
var csrf = require('csurf');
var csrfProtection = csrf();
//Using firebase initialized in config file.
var database = require('../config/firebase');
router.use(csrfProtection);
router.get('/login', function(req, res, next) {
res.render('login', { title: 'Login' });
});
router.get('/forgotpassword', function (req,res){
res.render('forgotpassword', {title:'Forgot Password'})
});
//ADD A TERMS PAGE TO SHOW TERMS AND CONDITIONS
router.get('/signup', function (req,res){
res.render('signup', {title:'Sign up', csrfToken: req.csrfToken()});
});
router.post('/signup', function(req, res, next){
res.redirect('/');
});
module.exports = router;
的index.js
正確渲染主視圖。其他路線,如/登錄,或/註冊都將404,我似乎無法弄清楚爲什麼。
的問題是'無功csrfProtection = CSRF();',您所呼叫的一個函數。而是嘗試'var csrfProtection = csrf;' – Hosar