龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > Javascript编程 >

使用node.js 制作网站前台后台_node.js(2)

时间:2014-11-16 16:12来源:网络整理 作者:网络 点击:
分享到:
复制代码 代码如下: exports.add = function(req, res) { var form = new formidable.IncomingForm(); form.uploadDir = path.join(__dirname, '../files'); form.keepExtensions = true; form.parse(req, 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();  //如果不存在就创建表。
      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:

复制代码 代码如下:

//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.   最初时候我使用截取字符串,也能实现,但是操作系统不一样的话就会有问题。mac使用'/' . window下面是'\\',我也是部署完成之后才发现的问题 。  后来发现path.basename  直接替换(文档阅读的少,就吃亏啊)。对node.js的好感在加1分。:)

3. redis 缓存经常查询,而且很少变化的数据。

复制代码 代码如下:

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

精彩图集

赞助商链接