一個提供課程心得的校園平台
- Install package
npm install
- Start server
npm start
- For development
npm run dev
├── app.js
├── assets
│ ├── css
│ └── js
├── routes
│ └── xxxx.js
│── model
│ └── db.js
└── views
├── public
└── xxxx.ejs
app.js
-> setting of the serverassets
-> custom css and js filecss
js
routes
-> setting of the routes (Controller)model
-> database functionviews
-> template files (View)public
-> layouts
操作session和cookie的方法
res.cookie('uid', 1, {maxAge: 60 * 1000});
req.session.uid=1
設置該頁面的layout
<% layout('../path/layout') %>
導入片段的 html or ejs
<% include path/view %>
index
-> GET: show allshow
-> GET: show singlenew
-> GET: new pagecreate
-> POST: add into dbedit
-> GET: edit pageupdate
-> POST: update into dbdelete
-> DEL: del data in db
url?id=1
req.query.id
url/:id
req.params.id
pass by table
req.body.id
請先加入
var db = require(./db);
datas就是資料庫回傳的資料
db.Insert(table,data,callback);
var data = {
'col1': 'value1',
'col2': 'value2',
};
db.Insert('table_name',data,function(err,result){...});
//result.insertId = the id of the data you just insert
db.DeleteById(table,id,callbak);
db.DeleteById('table_name','2',function(err){...});
db.GetAll(table,order,callback);
db.GetAll('table_name','columns_name',function(datas){...});
db.GetCols(table,columns,order,callback);
var columns = ['columns_name1','columns_name2'];
db.GetCols('table_name',columns,'columns_name',function(datas){...});
db.FindbyID(table,id,callback);
db.FindbyID('table_name','2',function(data){...});
db.FindbyColumn(table,conditions,callback);
var conditions ={
id:2,
name:'xxx'
};
db.FindbyColumn('user',conditions,function(datas){...});
db.FindbyColumnOrder(table,conditions,order,callback);
var conditions ={
user_id:2
};
db.FindbyColumnOrder('post',conditions,{'column':ordercolumn,'order':'ASC/DESC'},function(datas){...});
Update(table,datas,conditions,callback);
var datas ={
name:'newname',
nickname:'newnickname'
};
var conditions ={
id:3
};
db.Update('user',datas,conditions,function(results){...});
InnerJoin(tables,cols,conditions,callback);
var tables = {'table':'post','jointable':'user'};
var cols = ['post.title','user.name'];
var conditions = {'user.id':post.user_id};
db.InnerJoin(tables,cols,conditions,function(results){...});