安装node
1 $ npm install -g express 2 $ npm install -g express-generator 3 $ express myBlog 4 $ cd myBlog && npm install 5 $ npm start
npm start 之后就跑起来了,打开浏览器localhost 3000端口 http://localhost:3000/
-------------------------------------------------------------------------------------分割线
常用的express函数
res.redirect('/'); 重定向
express -e myblog 创建博客的时候使用ejs 默认为jade
1 router.post('/reg',function (req, res, next) { 2 var userName = req.body.username; //获取post后页面内的name 3 }
var session = require('express-session'); //引入session
1 //使用session 2 app.use(session({ 3 secret: 'myblog', 4 resave: false, 5 saveUninitialized: false 6 }));
app.use(flash()); //使用flash
//设置默认全局变量
1 app.use(function(req,res,next){ 2 res.locals.error = req.flash('error').toString() || ""; 3 res.locals.success = req.flash('success').toString() || ""; 4 res.locals.title = ""; 5 res.locals.count = 0; 6 res.locals.pageNum= 0; 7 res.locals.pageSize= 0; 8 res.locals.totalPage= 0; 9 res.locals.keyword=''; 10 res.locals.user = req.session.user; 11 next(); 12 });
app.locals:这是一个函数对象,可以给它增加新的属性。程序内所有页面模板都能访问这个对象,所以可以用它保存全局配置变量供页面模板使用。
调用方法
1 <%=title%> 2 <%=error%>