SoFunction
Updated on 2025-03-02

Use the website front desk backend

What can I do? I still don’t know what aspects he uses are quite widely, and I have no chance to come into contact with such projects. Just because I liked it, I made a website and backend in my spare time. I have deeply understood one truth that is, if you like a technology, you can play with it, but if you use it in a project, you must spend some time solving many problems.

Techniques used:

express + jade

sqlite + sequelize  

redis

1. About Jade

Support include. For example: include ./includes/header  header is a partial view, similar to a user control.

Support extends. For example: extends ../layout  Use the master page layout.

The for loop is so simple.

Copy the codeThe code is as follows:

each item in userList  (the variable passed to the front end by the userList server)
tr
  td #{}
  td #{}
  td #{}

I prefer append:

Copy the codeThe code is as follows:

extends ../admin_layout
append head
  link(rel='stylesheet', href='/stylesheets/')
  script(src='/javascripts/')
  script(src='/javascripts/')
  script(src='/javascripts/')
block content

append will place all steps and styles behind the head of the master page.

The framework for implementing ORM. Support sqlite mysql mongodb

Defining the model (article):

Copy the codeThe code is as follows:

var Article = ('Article',{
  title:{
    type:,
    validate:{}
  },
  content:{type:,validate:{}},
  icon:{type:,validate:{}},
  iconname:{type:},
  sequencing:{type:,validate:{}}
},{
  classMethods:{
//Article classification
    getCountAll:function(objFun){
    }//end getCountAll
  }//end classMethods
});
(Category);

(Category);  Each article has a category.

I wrote the paging-related methods to the time when initializing sequelize. In this way, there will be this method (pageOffset, pageLimit) when defining each model.

Copy the codeThe code is as follows:

var sequelize = new Sequelize('database', 'username', 'password', {
  // sqlite! now!
  dialect: 'sqlite',
  // the storage engine for sqlite
  // - default ':memory:'
  storage: ,
  define:{
    classMethods:{
      pageOffset:function(pageNum){
        if(isNaN(pageNum) || pageNum < 1){
          pageNum = 1; 
        }
        return (pageNum - 1) * ();
      },
      pageLimit:function(){
return 10; // 10 items are displayed on each page
      },
      totalPages:function(totalNum){
        var total =parseInt((totalNum + () - 1) / ()),
            arrayTotalPages = [];
        for(var i=1; i<= total; i++){
          (i);
        }
        return arrayTotalPages;
      }
    },
    instanceMethods:{
    }
  }
});

use:

Copy the codeThe code is as follows:

({include:[Category],offset:(), limit:()}).success(function(row){
    ('article_list', {
title: 'Article Management',
      articleList : , 
      pages:{
        totalPages:(),
        currentPage:,
        router:'article'
      }
    });
  });

Save the model:

Copy the codeThe code is as follows:

= function(req, res) {
  var form = new ();
  = (__dirname, '../files');
  = true;
  (req, function(err, fields,files){
    var //iconPath = ,
        //index = ('/') <= 0 ? ('\\') : ('/') ,
        icon = (), // (index + 1, - index),
        iconname = ;
    var title = ;
        id = ;
        title = ,
        content = ,
        mincontent = ,
        sequencing= == 0 ? 0 : 1,
        category = ;
();  //Create a table if it does not exist.
      (category).success(function(c){
        var article = ({
          title : title,
          content:content,
          mincontent:mincontent,
          icon:icon,
          iconname:iconname,
          sequencing:sequencing
        });
        ()
        .success(function(a){
          (c);
          return ('/admin/article');
        });
      }); //end category
  });
}

:

Copy the codeThe code is as follows:

//iconPath = ,
//index = ('/') <= 0 ? ('\\') : ('/') ,
icon = <strong></strong>(), // (index + 1, - index),

Get the file name, such as: /a/b/ => .   I used intercepting strings at first, which could also be implemented, but if the operating system is different, there will be problems. mac uses '/' . window is below '\\', and I also discovered the problem after the deployment is completed. Later I found out that it was directly replaced (if you read the document less, you will suffer a loss). The right favor is 1 point. :)

3. Redis caches frequently queries and rarely changes data.

Copy the codeThe code is as follows:

getCountAll:function(objFun){
      ('articles_getCountAll', function(err,reply){
        if(err){
          (err);
          return;
        }
        if(reply === null){
          ('SELECT count() as count,, FROM articles left join categories on = group by ', function(err,row){
            ('articles_getCountAll',(row));
            objFun(row);
          });
        }else{
          objFun(reply);
        }
      });

This method is defined in the model layer. Because it is express, use the mvc method to develop as much as possible. In fact, route implements the controller layer function (route folder, which should be named controller).