server.js
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(8080)
//获取前台请求数据
server.use(multerObj.any())
//cookie,session
(function(){
server.use(cookieParser());
var keys=[]
for(var i=0;i<100000;i++){
keys[i]='a_'+Math.random()
}
server.use(cookieSession({
name: 'sess_id',
keys: keys,
maxAge: 20*60*1000
}))
})()
//模板
server.engine('html', consolidate.ejs)
server.set('views', 'template')
server.set('view engine', 'html')
// route
var r1 = express.Router()
var r2 = express.Router()
server.use('/article', r1)
r1.get('/1.html', function(req,res){
res.send('我是文章').end()
})
r1.get('/2.html', function(req,res){
res.send('我ye是文章').end()
})
server.use('/blog', r2)
r2.get('/a.html', function(req,res) {
res.send('我是blog').end()
})
r2.get('/b.html', function(req,res) {
res.send('我也是blog').end()
})
// default: static
server.use(static('./static/'));
pack.json
{
"devDependencies": {
"body-parser": "^1.18.2",
"consolidate": "^0.15.0",
"cookie-parser": "^1.4.3",
"cookie-session": "^2.0.0-beta.3",
"express": "^4.16.2",
"express-route": "^0.1.4",
"express-static": "^1.2.5",
"multer": "^1.3.0",
"mysql": "^2.15.0",
"static": "^2.0.0"
}
}
安装的时候会报错
运行的时候会报错