欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用node.js 制作網(wǎng)站前臺(tái)后臺(tái)

 更新時(shí)間:2014年11月13日 16:33:27   投稿:hebedich  
本文給大家介紹實(shí)用node.js 制作網(wǎng)站前臺(tái)和后臺(tái),非常的詳盡,有需要的朋友可以參考下

node.js  能做什么?我至今也不清楚,他在哪方面應(yīng)用比較廣泛,我沒有機(jī)會(huì)接觸到那樣的項(xiàng)目。只是因?yàn)橄矚g,業(yè)余時(shí)間做了一個(gè)網(wǎng)站和后臺(tái)。深刻領(lǐng)悟到一個(gè)道理那就是如果你喜歡一項(xiàng)技術(shù)可以玩玩,但是如果用到項(xiàng)目中就必須花些時(shí)間去解決很多問題。

使用到的技術(shù):

express + jade

sqlite + sequelize  

redis

1. 關(guān)于jade

    支持include。  比如: include ./includes/header  header 是一個(gè)局部視圖,類似asp.net  用戶控件。

    支持extends。 比如: extends ../layout   使用母版頁(yè)layout。

    for循環(huán)也是如此簡(jiǎn)單。   

復(fù)制代碼 代碼如下:

each item in userList  (userList 服務(wù)器傳給前端的變量)
tr
  td #{item.username}
  td #{item.telephone}
  td #{item.email}

  比較喜歡append:

復(fù)制代碼 代碼如下:

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 會(huì)把腳步和樣式全部放在 母版頁(yè)面head后面。

2.sequelize  實(shí)現(xiàn)ORM的框架。 支持sqlite mysql mongodb

   定義模型(文章):

復(fù)制代碼 代碼如下:

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:{
    //文章分類
    getCountAll:function(objFun){
    }//end getCountAll
  }//end classMethods
});
Article.belongsTo(Category);

 Article.belongsTo(Category);  每一篇文章都有一個(gè)分類。

我把分頁(yè)相關(guān)方法寫到了初始化sequelize時(shí)候。這樣每個(gè)模型定義時(shí)候,都會(huì)有這個(gè)方法(pageOffset、pageLimit)。

復(fù)制代碼 代碼如下:

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; //每頁(yè)顯示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:{
    }
  }
});

使用:

復(fù)制代碼 代碼如下:

Article.findAndCountAll({include:[Category],offset:Article.pageOffset(req.query.pageNum), limit:Article.pageLimit()}).success(function(row){
    res.render('article_list', {
      title: '文章管理',
      articleList : row.rows, 
      pages:{
        totalPages:Article.totalPages(row.count),
        currentPage:req.query.pageNum,
        router:'article'
      }
    });
  });

保存模型:

復(fù)制代碼 代碼如下:

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();  //如果不存在就創(chuàng)建表。
      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.basename:

復(fù)制代碼 代碼如下:

//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),

獲取文件名,比如:/a/b/aa.txt   => aa.txt.   最初時(shí)候我使用截取字符串,也能實(shí)現(xiàn),但是操作系統(tǒng)不一樣的話就會(huì)有問題。mac使用'/' . window下面是'\\',我也是部署完成之后才發(fā)現(xiàn)的問題 。  后來(lái)發(fā)現(xiàn)path.basename  直接替換(文檔閱讀的少,就吃虧?。?。對(duì)node.js的好感在加1分。:)

3. redis 緩存經(jīng)常查詢,而且很少變化的數(shù)據(jù)。

復(fù)制代碼 代碼如下:

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);
        }
      });

    這個(gè)方法定義在了 model層。 因?yàn)槭莈xpress,所以盡可能的 用mvc方式開發(fā)。 其實(shí)是route實(shí)現(xiàn)了controller層功能(route文件夾,應(yīng)該命名為為controller)。

相關(guān)文章

最新評(píng)論