Use node.js to create the foreground and background of the website
- 2020-03-30 04:19:08
- OfStack
Node. Js What can be done? I still don't know where he is widely used, I have no chance to contact with such a project. Just because I like it, I made a website and backstage in my spare time. I've learned that if you like a technology you can play with it, but if you use it on a project you have to spend some time solving a lot of problems.
Techniques used:
Express + jade
Sqlite + sequelize
redis
1. About the jade
So is the for loop.
each item in userList ( userList Server to front-end variables)
tr
td #{item.username}
td #{item.telephone}
td #{item.email}
I prefer append:
extends ../admin_layout
append head
link(rel='stylesheet', href='/stylesheets/font-awesome.css')
script(src='/javascripts/bootstrap.js')
script(src='/javascripts/bootstrap-wysiwyg.js')
script(src='/javascripts/jquery.hotkeys.js')
block content
Append puts all the steps and styles behind the master page head.
Define the model (article) :
var Article = sequelize.define('Article',{
title:{
type:Sequelize.STRING,
validate:{}
},
content:{type:Sequelize.STRING,validate:{}},
icon:{type:Sequelize.STRING,validate:{}},
iconname:{type:Sequelize.STRING},
sequencing:{type:Sequelize.STRING,validate:{}}
},{
classMethods:{
//Article classification
getCountAll:function(objFun){
}//end getCountAll
}//end classMethods
});
Article.belongsTo(Category);
The Article. The belongsTo (Category); Each article has a category.
I wrote the page-related method to initialize sequelize. This method (pageOffset, pageLimit) will be used for each model definition.
var sequelize = new Sequelize('database', 'username', 'password', {
// sqlite! now!
dialect: 'sqlite',
// the storage engine for sqlite
// - default ':memory:'
storage: config.sqlitePath,
define:{
classMethods:{
pageOffset:function(pageNum){
if(isNaN(pageNum) || pageNum < 1){
pageNum = 1;
}
return (pageNum - 1) * this.pageLimit();
},
pageLimit:function(){
return 10; //Each page displays 10
},
totalPages:function(totalNum){
var total =parseInt((totalNum + this.pageLimit() - 1) / this.pageLimit()),
arrayTotalPages = [];
for(var i=1; i<= total; i++){
arrayTotalPages.push(i);
}
return arrayTotalPages;
}
},
instanceMethods:{
}
}
});
Use:
Article.findAndCountAll({include:[Category],offset:Article.pageOffset(req.query.pageNum), limit:Article.pageLimit()}).success(function(row){
res.render('article_list', {
title: ' The article management ',
articleList : row.rows,
pages:{
totalPages:Article.totalPages(row.count),
currentPage:req.query.pageNum,
router:'article'
}
});
});
Save the model:
exports.add = function(req, res) {
var form = new formidable.IncomingForm();
form.uploadDir = path.join(__dirname, '../files');
form.keepExtensions = true;
form.parse(req, function(err, fields,files){
var //iconPath = files.icon.path,
//index = iconPath.lastIndexOf('/') <= 0 ? iconPath.lastIndexOf('\') : iconPath.lastIndexOf('/') ,
icon = path.basename(files.icon.path), // iconPath.substr(index + 1,iconPath.length - index),
iconname = files.icon.name;
var title = fields.title;
id = fields.articleId;
title = fields.title,
content = fields.content,
mincontent = fields.mincontent,
sequencing=fields.sequencing == 0 ? 0 : 1,
category = fields.category;
Article.sync(); //Create the table if it doesn't exist. < br / >
Category.find(category).success(function(c){
var article = Article.build({
title : title,
content:content,
mincontent:mincontent,
icon:icon,
iconname:iconname,
sequencing:sequencing
});
article.save()
.success(function(a){
a.setCategory(c);
return res.redirect('/admin/article');
});
}); //end category
});
}
Path. The basename:
//iconPath = files.icon.path,
//index = iconPath.lastIndexOf('/') <= 0 ? iconPath.lastIndexOf('\') : iconPath.lastIndexOf('/') ,
icon = <strong>path.basename</strong>(files.icon.path), // iconPath.substr(index + 1,iconPath.length - index),
Gets the file name, such as /a/b/aa.txt = > Aa. TXT. Initially I used intercepting strings, which I could do, but with a different operating system there were problems. The MAC USES '/'.window followed by '\ ', which I discovered after deployment. Path. Basename&cake; Direct substitution (if you don't read enough, you'll lose). Good feelings for node.js add 1 point. :)
3. Redis cache frequently queries, and seldom changes the data.
getCountAll:function(objFun){
redis.get('articles_getCountAll', function(err,reply){
if(err){
console.log(err);
return;
}
if(reply === null){
db.all('SELECT count(articles.CategoryId) as count,categories.name,categories.id FROM articles left join categories on articles.categoryID = categories.id group by articles.CategoryId ', function(err,row){
redis.set('articles_getCountAll',JSON.stringify(row));
objFun(row);
});
}else{
objFun(reply);
}
});