求助 express为什么无法跳转
发布于 12 年前 作者 jtyjty99999 6285 次浏览 最后一次编辑是 8 年前

我想先尝试访问网站时时跳转到login,然后就这么写

var express = require('express'),
    routes = require('./routes'),
    http = require('http'),
    path = require('path'),
    fs = require('fs'),
    juicer = require('juicer'),
    config = require('./config').config;

var app = express();

var staticDir = path.join(__dirname, 'public');
app.configure(function () {
    app.set('port', config.port || 3000);
    app.set('views', __dirname + '/views');
    app.set('view engine', 'html');
    app.engine('html', function (path, options, fn) {//利用juicer作为模板引擎
        fs.readFile(path, 'utf8', function (err, str) {
            if (err)
                return fn(err);
            str = juicer(str, options);
            fn(null, str);
        });
    });
    app.use(express.favicon());
    app.use(express.logger('dev'));
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(app.router);
    app.use(express.cookieParser());//开启cookie
    app.use(express.session({//开启session
        secret: config.session_secret
    }));
});

routes(app);

app.use(function (req, res, next) {
    var url = req.originalUrl;
    console.log(url);
    if (url != "/login" && !req.session.user) {//拦截
        res.redirect('/login')
        console.log(1)
    }
    next();
});

app.configure('development', function () {
    app.use(express.errorHandler());
});
app.configure('production', function () {
    app.use(express.static(staticDir, { maxAge: 3600000 * 24 * 30 }));//静态文件支持加缓存
    app.use(express.errorHandler());
    app.set('view cache', true);
});
http.createServer(app).listen(app.get('port'), function () {
    console.log("Express server listening on port " + app.get('port'));
});

然后router是

var site = require('../controller/site');

module.exports = function(app){

    app.get('/', site.index);
    app.get('/home', site.home);
    app.get('/login', site.showLogin);
    //app.post('/login', site.login);
}

直接访问login是可以访问的,但是为什么访问主页,或者任意一个页面跳转不到login呢

2 回复

已经解决了,抱歉

请教楼主,如何解决的?我现在是卡在拦截成功,但是登录之后一直在拦截页面

回到顶部