var express = require('express'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var partials = require('express-partials'); var settings = require('./settings'); var flash = require('connect-flash'); var session = require('express-session'); var MongoStore = require('connect-mongo')(session); var routes = require('./routes/index'); var users = require('./routes/users'); var square = require('./routes/square'); var app = express(); //var server = require('http').createServer(app); var io = require('socket.io').listen(8888); //server.listen(8888); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs'); app.use(partials()); app.use(flash()); // uncomment after placing your favicon in /public //app.use(favicon(__dirname + '/public/favicon.ico')); app.use(logger('dev')); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(express.static(path.join(__dirname, 'public'))); app.use(cookieParser('stel')); //Set session app.use(session({ resave: false, saveUninitialized: true, cookie:{maxAge:3600000}, secret:settings.cookieSecret, store:new MongoStore({ db:settings.db, url: settings.host }) })); /*app.use(function(req, res, next){ io.sockets.on('connection', function (socket) { //flash ability if(typeof req.session.err != undefined) { socket.emit('Err',req.session.err); delete req.session.err; } else { socket.emit('Err',req.session.err); }; if(typeof req.session.success != undefined) { console.log("success: ",req.session.success); socket.emit('Success',req.session.success); delete req.session.success; } else { socket.emit('success',req.session.success); }; }); next(); });*/ function notIFlogin(req, res, next) { if (!req.session.user) { req.session.err='请先登陆'; return res.redirect('/login'); } next(); } function IFlogin(req, res, next) { if (req.session.user) { req.session.err='您已登陆'; return res.redirect('/'); } next(); } app.use(function(req, res, next){ //Deal with err or success var err = req.session.err; var success = req.session.success; var iflogin = req.session.user; var ifn_words = 1, n_words = 0; var ifr_words = 1, r_words = 0; delete req.session.err; delete req.session.success; res.locals.message = ''; res.locals.iflogin = ''; if (err) res.locals.message = '