类似MEAN Stack,但是网页模板使用较简单的EJS
只记录大架构
先安装Express 应用程式产生器
点击(此处)折叠或打开
- $ npm install express-generator -g
使用产生器快速生成一个网站,并push到heroku
点击(此处)折叠或打开
- //--git add .gitignore
- $ express myexpress2 --view ejs --git
- $ cd myexpress2
- $ npm install
- $ git init
- $ git add .
- $ git commit -am "init"
- $ heroku create myexpress2
- $ heroku git:remote -a myexpress2
- $ git push heroku master
新增两个资料夹controllers、models可以放置业务逻辑跟资料模型
mongoose.Schema放在models,CURD放在controllers里
在views新增资料夹partials放置ejs共用模板
在app.js连接资料库并设定路由
点击(此处)折叠或打开
- $ npm install express-session --save
- // app.js 啟用session
- var session = require('express-session');
- app.use(session({ secret: 'test', resave: true, saveUninitialized: true, cookie: { maxAge: 14400000 }}));
- app.use(express.static(path.join(__dirname, 'public')));
- app.use(function(req, res, next) {
- res.locals.logined = req.session.logined;
- res.locals.username = req.session.username;
- next();
- })