작업 전에 로그인 구현

1714 단어
const express=require('express');

module.exports=function(){
  var router=express.Router();

  // 
  router.use(function(req,res,next){
    //  , 
    if(!req.session['admin_id']&&req.url!='/login'){
      res.redirect('/admin/login');
    }else{
      next();
    }
  });

  router.get('/login',function(req,res){
    res.render('admin/login.ejs',{});
  });
  return router;
};
const express=require('express');
const static=require('express-static');
const bodyParser=require('body-parser');
const multer=require('multer');
const multerObj=multer({dest:'./static/upload'});
const mysql=require('mysql');
const cookieParser=require('cookie-parser');
const cookieSession=require('cookie-session');
const consolidate=require('consolidate');
const expressRoute=require('express-route');

var server=express();
server.listen(3000);

// 1. 
//get 
server.use(multerObj.any());

// 2.cookie、session
server.use(cookieParser());
(function(){
  var keys=[];
  for(var i=0;i<1000;i++){
    keys[i]='a'+Math.random();
  }
  server.use(cookieSession({
    name:'sess_id',
    keys:keys,
    maxAge:20*60*1000
  }));
})();

// 3.template
//  html 
server.set('view engine','html');
//  
server.set('views','./template');
//  ejs html, 
server.engine('html',consolidate.ejs);

// 4.route
server.use('/',require('./route/web.js')());
server.use('/admin/',require('./route/admin.js')());

// 5.default:static
server.use(static('./static/'));

좋은 웹페이지 즐겨찾기