基本server配置
cookie / session / get数据 / post数据 / 请求方法
const express=require('express'); const static=require('express-static'); const cookieParser=require('cookie-parser'); const cookieSession=require('cookie-session'); const bodyParser=require('body-parser'); const multer=require('multer'); const consolidate=require('consolidate'); var server=express(); server.listen(8080); //1.解析cookie server.use(cookieParser('sdfasl43kjoifguokn4lkhoifo4k3')); //2.使用session var arr=[]; for(var i=0;i<100000;i++){ arr.push('keys_'+Math.random()); } server.use(cookieSession({name: 'zns_sess_id', keys: arr, maxAge: 20*3600*1000})); //3.post数据 server.use(bodyParser.urlencoded({extended: false})); server.use(multer({dest: './www/upload'}).any()); //4.配置模板引擎 //输出什么东西 server.set('view engine', 'html'); //模板文件放在哪儿 server.set('views', './views'); //哪种模板引擎 server.engine('html', consolidate.ejs); //接收用户请求 server.get('/index', function (req, res){ res.render('1.ejs', {name: 'blue'}); }); //4.static数据 server.use(static('./www'));
命令行
npm install express express-static cookie-parser cookie-session body-parser multer consolidate
基本路由配置
express路由功能
const express=require('express'); var server=express(); //目录1:/user/ var routeUser=express.Router(); routeUser.get('/1.html', function (req, res){ //http://xxx.com/user/1.html res.send('user1'); }); routeUser.get('/2.html', function (req, res){ //http://xxx.com/user/2.html res.send('user22222'); }); server.use('/user', routeUser); //目录2:/article/ var articleRouter=express.Router(); server.use('/article', articleRouter); articleRouter.get('/10001.html', function (req, res){ //http://xxxx.com/article/10001.html res.send('asdfasdfasdf'); }); server.listen(8080);
版权声明:除特别声明外,本站所有文章皆是本站原创,转载请以超链接形式注明出处!