启动index.js:
var express = require("express"); var path = require("path"); var open = require("child_process"); var port = process.env.PORT || 3000; var cookieParser = require('cookie-parser'); //bodyParser 的功能是解析客户端请求,通常是通过 POST 发送的内容 var bodyParser = require('body-parser'); var favicon = require("serve-favicon"); var logger = require('morgan'); var ejs = require('ejs'); //由于新版本不支持layout.ejs,则需要引入express-partials模块 var partials = require('express-partials'); var MongoStore = require('connect-mongo'); var settings = require("./settings"); var app = express(); app.set(port,3000); //设置引擎jade //app.set("views","./views"); //app.set("view engine","jade"); // 定义EJS模板引擎和模板文件位置,也可以使用jade或其他模型引擎 app.set("views",path.join(__dirname,'ejviews')); //app.set("view engine","ejs"); //注册html模板引擎 将模版页后缀换成.html http://segmentfault.com/a/1190000000598557 app.engine('.html',ejs.__express); //将模板引擎换成html app.set('view engine', 'html'); //app.js 的中 app.configure 中添加以下内容,这样页面布局功能可以关闭和启用layout功能 app.set('view options', { layout: true }); //定义icon图标 app.use(favicon(__dirname + '/public/images/icon.png')); //定义日志和输出级别 app.use(logger('dev')); // 定义数据解析器 app.use(bodyParser.json()); app.use(bodyParser.urlencoded({extended : false})); // 定义cookie解析器 app.use(cookieParser()); //定义静态文件目录 app.use(express.static(path.join(__dirname,'public'))); app.use(partials()); //设置路由 app.get('/',function(req,res){ res.redirect("/home"); res.render('index',{title:'imooc home',name:"pfan",content:"大家好,我来自火星,我的任务是奋斗!",layout:"layout"}); res.send('The time is ' + new Date().toString()); }); app.get('/user',function(req,res,next){ res.writeHead(200,{"Content-Type":"text/html;charset=UTF-8"}); res.write("hello world"); res.write("hello world"); console.log(req.body.title); res.send(req.body.title + req.body.text); res.end(); //Express 提供了路由控制权转移的方法,即回调函数的第三个参数next,通过调用next(),会将路由控制权转移给后面的规则 next(); }); app.get('/home',function(req,res,next){ res.render('home',{title:'imooc home',name:"pfan",content:"大家好,我来自火星,我的任务是奋斗!",layout:"layout"}); }); var server = app.listen(3000,function(){ console.log("listening on port %d",server.address().port); }); open.exec("start http://127.0.0.1:3000");
路由模版:
layout.html
<!DOCTYPE html> <html> <head> <title><%= title %></title> <meta charset="UTF-8"> <meta name="keywords" content="express"> <meta name="description" content="express"> <meta name="author" content="pingfan"> <link rel='stylesheet' href='/stylesheets/style.css' /> </head> <body> <%- body %> </body> </html>
header.html
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title><%= title %></title> 5 <meta charset="UTF-8"> 6 <meta name="keywords" content="express"> 7 <meta name="description" content="express"> 8 <meta name="author" content="pingfan"> 9 <link rel='stylesheet' href='/stylesheets/style.css' /> 10 </head> 11 <body>
footer.html
<link rel='stylesheet' href='/javascripts/fastclick.js' /> </body> </html>
home.html
<% include header.html %> <h1><%= title %></h1> <p>Welcome to <%= name %></p> <div class="red_txt"> <%= content %> </div> <h2>你大爷的,你不做会死啊</h2> <% include footer.html %>
参考资料:
cookie 和 session: https://github.com/alsotang/node-lessons/tree/master/lesson16
expressjs session配置: http://droiz.me/2015/02/21/session%E9%85%8D%E7%BD%AE/
Express 4.x Node.js的Web框架: http://blog.csdn.net/u013758116/article/details/38703381
express session 不同步问题: http://www.lellansin.com/express-session-%E4%B8%8D%E5%90%8C%E6%AD%A5%E9%97%AE%E9%A2%98.html
Session原理、安全以及最基本的Express和Redis实现: http://segmentfault.com/a/1190000002630691