--------------14章-------------
01、ejs
ejs——非侵入、非破坏
<%= name %>
<%= json.arr[0].name %>
<%= 12+5 %>
<%= 'asdfasdf' %>
1.js文件
const ejs = require('ejs')
ejs.renderFile('./views/1.ejs', {name: '夜幕小草',
json: {arr: [
{user: 'blue', pass: '123456'},
{user: 'zhangsan', pass: '654321'},
{user: 'xiaoming', pass: '999999'},
]}
}, (err, data) => {
console.log(data)
})
1.ejs文件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div>
<%= name %>
</div>
<div>
<%= json.arr[1].user%>
</div>
<div>
<!-- 输出的时候才是<%= %> ,不是输出的时候没有等号的<% %> -->
<% for(var i=0;i<json.arr.length;i++){ %>
<div>用户名:<%=json.arr[i].user%> 密码:<%=json.arr[i].pass%></div>
<% } %>
</div>
<div>
<% var str="<div>夜幕小草2</div>";
%>
<!-- 输出是标签的时候要用<%- str %> 是‘-’而不是‘=’ -->
<%- str %>
</div>
<div>
<% for(var i=0;i<5;i++){ %>
<!-- 引入文件的时候用include,并且文件名字不能用变量, 如a.text中a不能是变量 -->
<% include ./a.txt %>
<% } %>
</div>
</body>
</html>
-----------15章---------------
02、笔记
express:
1.主体
2.cookie、session
3.数据
4.模板引擎
body-parser 解析post数据
multer 解析post文件
--------------------------------------------------------------------------------------------------------------
body-parser 解析post数据 application/x-www-form-urlencoded
server.use(bodyParse.urlencode());
req.body
multer 解析post文件 multipart/form-data
var obj=multer({dest: 'upload/'});
server.use(obj.any());
server.use(function (req, res){
req.files[0].originalname
req.files[0].path
});
把扩展名加上
//'upload/aadfaew324we34' + '.txt'
//'upload/aadfaew324we34.txt'
var newName=file.path+pathLib.parse(file.originalname).ext;
fs.rename(老名, 新名, function (err){});
------------------16章---------------
express整合
body-parser
multer
模板引擎
express 模板引擎
consolidate-适配
-------------------------------------------------------------------------------------------------------------------------
模板引擎:适配
1.consolidate
consolidate=require
server.set('view engine', 'html');
server.set('views', '模板文件目录');
server.engine('html', consolidate.ejs);
server.get('/', function (req, res){
res.render('模板文件', 数据);
});
-------------------------------------------------------------------------------------------------------------------------
route-路由:
把不同的目录,对应到不同的模块
xxxx.com/aaa/ mod1
xxxx.com/news/ mod_news
post news_post
list news_list
content news_content
xxxx.com/user/ mod_users
-------------------------------------------------------------------------------------------------------------------------
server.get();
server.use()
server.post()
Router——迷你server
router.get();
router.post();
router.use();
-------------------------------------------------------------------------------------------------------------------------
Router——拆
/user/.... mod_user
/item/.... mod_item
//1.创建router
var router=express.Router();
//2.把router添加到server
server.use('/user', router);
//3.router内部
router.get('/1.html')
router.post('/2.html')
-------------------------------------------------------------------------------------------------------------------------
consolidate
server.set('view engine', 'html');
server.set('views', '模板目录');
server.engine('html', consolidate.ejs);
server.use(function (req, res){
res.render('模板文件', 数据);
});
-------------------------------------------------------------------------------------------------------------------------
Router——子服务
var router1=express.Router();
server.use('/user', router1);
var r=express.Router();
router1.use('/user_mod', r);
router1.use('/user_reg', function (){});
http://www.xxxx.com/user/user_mod
http://www.xxxx.com/user/user_reg
http://www.xxxx.com/user/user_login
var router2=express.Router();
server.use('/news', router2);
http://www.xxxx.com/news/list
http://www.xxxx.com/news/post
http://www.xxxx.com/news/content
var router3=express.Router();
server.use('/item', router3);
http://www.xxxx.com/item/buy
http://www.xxxx.com/item/mod
http://www.xxxx.com/item/del
----------------------------------
网友评论