const express=require('express');
const expressRoute=require('express-route');
const static=require('express-static');
const mysql=require('mysql');
const cookieParser=require('cookie-parser');
const cookieSession=require('cookie-session');
const bodyParser=require('body-parser');
const multer=require('multer');
const multerObj=multer({dest:'./staic/upload/'})
const consolidate=require('consolidate');
const server=express();
server.listen(8080);
1.获取前台请求数据
get query自带
post-bodyparser,multer
server.use(bodyParser.urlencoded({extended:false}));
server.use(multerObj.any());
- 2.cookie,session
server.use(cookieParser());
(function(){
var keys=[];
for(vari=0;i<100000;i++){
keys[i]='a_'+Math.random();
}
server.use(cookieSession({
name:'sess_id',
keys,
maxAge:20*60*1000
}));
})();
- 3.模版
server.engine('html',consolidate.ejs);//用哪个模版来渲染html;
server.set('views','./template');//设置模版目录
server.set('view engine','html');//设置模版的展现方式,是html
- 4.route:route不能用get,只能用use;即添加路由的时候,不能用get,只能用use
//require('./route/1.js') 和 require('./route/2.js') 是自己封装的中间件
server.use('/article/',require('./route/1.js')());
server.use('/blog/',require('./route/2.js')());
- 5.default:static
server.use(static('./static/'));
- require('./route/1.js') 和 require('./route/2.js') 中间件代码:
const express=require('express');
module.exports=()=>{
var router=express.Router();
router.get('/1.html',(req,res)=>{
res.render('./1.ejs',{
a:1,
b:2,
title:'测试'
})
});
router.get('/2.html',(req,res)=>{
res.send('我是文章2').end();
});
return router;
};