liuhaifeng315的gravatar头像
liuhaifeng315 2017-01-05 16:17:02

node js+mongodb整合开发简单增删改查实例,适合nodejs初学者学习

今天弄了一个nodejs+mongodb 项目,特地的把我做的功能分享出来.自己搭建好mongodb和node.js环境

node js+mongodb整合开发简单增删改查实例,适合nodejs初学者学习

这个是查询界面 默认的访问地址是localhost:3000/admin

项目目录

node js+mongodb整合开发简单增删改查实例,适合nodejs初学者学习

配置说明

app.js 最下面

// connect to the database
mongoose.connect('mongodb://192.168.1.11/data_collect');  为连接mongodb

mongodb表创建可以先创建实体

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var StrategySehema=new Schema({
    mac: String,
    sn:String,
    region:String,
    sw:String,
    hw:String,
    enable: {type:Number,default:0},
    confUpdateMode: {type:Number,default:0},
    confUpdatePeriod: {type:Number,default:0},
    protocol:   String,
    period: {type:Number,default:0},
    actionMax: {type:Number,default:0},
    resendNum: {type:Number,default:0},
    sendArray: Array,
    extralArray:Array

});
mongoose.model("strategy", StrategySehema)
var StrategyContent = mongoose.model('strategy');

然后在页面通过表单

form(action="/strategyForm", method="post") 提交数据

被app.js拦截

//策略配置保存
app.post('/strategyForm/',strategy.save);

进入strategy路由的save方法取得表单数据

var StrategyContent = require('./../dao/StrategyContent.js');

/**
 * 保存
 */
exports.save = function(req, res) {
    req.setEncoding("utf-8");
    var f_mac=req.body.mac;
    var f_sn=req.body.sn;
    var f_region=req.body.region ? req.body.region:'';
    var f_sw=req.body.sw ? req.body.sw:'';
    var f_hw=req.body.hw ? req.body.hw:'';
    var f_enable=req.body.enable ? req.body.enable:0;
    var f_confUpdateMode=req.body.confUpdateMode ? req.body.confUpdateMode:0;
    var f_confUpdatePeriod=req.body.confUpdatePeriod;
    var f_protocol=req.body.protocol;
    var f_period=req.body.period;
    var f_actionMax=req.body.actionMax;
    var f_resendNum=req.body.resendNum;
    var f_sendArray={};
    var sendArray=req.body.sendArray;
    if(sendArray.indexOf(",")>0){
        f_sendArray=sendArray.split(",");
    }else{
        f_sendArray=sendArray;
    }
    var f_extralArray={};
    var extralArray=req.body.extralArray;
    if(extralArray.indexOf(",")>0){
        f_extralArray=extralArray.split(",");
    }else{
        f_extralArray=extralArray;
    }
    var f_id=req.body.objId;
    console.log(f_id);
    try {
        var data={};
        data.mac=f_mac;
        data.sn=f_sn;
        data.region=f_region;
        data.sw=f_sw;
        data.hw=f_hw;
        data.enable= f_enable;
        data.confUpdateMode=f_confUpdateMode;
        data.confUpdatePeriod=f_confUpdatePeriod;
        data.protocol=f_protocol;
        data.period=f_period;
        data.actionMax=f_actionMax;
        data.resendNum=f_resendNum;
        data.sendArray=f_sendArray;
        data.extralArray=f_extralArray;
        if(f_id==''||f_id==null){
            StrategyContent.save(data,function(err,docs){
                console.log(err);
            });
        }else{
            data._id=f_id;
            StrategyContent.update(data,function(err,docs){
                console.log(err);
            });
        }
    } catch (e) {
        console.log(e);
        res.redirect('/admin');
    }
    res.redirect('/admin');
}

进入dao层StrategyContent.js保存

exports.save=function(params,callback){
    StrategyContent.collection.insert(params,function(err,docs){
        console.log(err);
    });
}

具体的可以下载代码看下


打赏

文件名:data_collect.zip,文件大小:6225.055K 下载
  • /
      • /data_collect
          • /data_collect/.idea
            • /data_collect/.idea/.name
            • /data_collect/.idea/data_collect.iml
            • /data_collect/.idea/jsLibraryMappings.xml
              • /data_collect/.idea/libraries
                • /data_collect/.idea/libraries/data_collect_node_modules.xml
            • /data_collect/.idea/misc.xml
            • /data_collect/.idea/modules.xml
            • /data_collect/.idea/vcs.xml
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友