Nodjs尝试(Windows7)

刚忙完公司项目,把以前了解的nodejs实践一下:

把需要的包/模块安装上:

1、安装上nodejs
2、安装npm包---**这个非常重要**
3、安装开发模块:
    mysql---npm install [-g] node-mysql, npn install [-g] mysql
    express---npm install [-g] express
    jade---npm install [-g] jade[模板模块]
其他模块根据需求添加吧!
    

 根据了解的nodejs基础,列出需要注意的几点:

       

1、模块问题:---模块继承
    var Dao = function () {
        this.className = "Dao";
        var mysql = require('mysql');
       var pool = mysql.createPool({
           host: 'localhost',
           user: 'wenshuang3',
           password: '13746.com',
           database: 'cai',
           port: 3306
       });
       this.pool = pool;//连接池
       this.insert = function (params) {
       }; //插入
       this.update = function (params) {
       }; //更新
       this.delete = function (params) {
       }; //删除
       this.select = function (params) {
       }; //查询
   };
   module.exports = new Dao();
   ==================================

    var dao = require('./Dao');
    var UserDao = function() {
	    this.className = "UserDao";
    };
    UserDao.prototype = dao;
    module.exports = new UserDao();

 

2、router回调问题
    var express = require('express');
var router = express.Router();
var userService = require("../serv/service/impl/UserServiceImpl");
/* POST home page. */
router.post('/', function (req, res) {
    var name = req.body.name;
    var age = req.body.age;
    var opt = req.body.type.toString();
    console.log(name + "==" + age + "---" + opt);
    console.log(typeof opt + "---" + typeof "select" + "==" + (opt == "select" ));
    var send_data = {
        name: name,
        age: age,
        callback: function (data) {
            res.render('userinfo', data);
        }
    };
    if (opt == "insert") {
        console.log("insert...");
        userService.insert(send_data);
    } else if (opt == "update") {
        console.log("update...");
        userService.update(send_data);
    } else if (opt == "delete") {
        console.log("delete...");
        userService.delete(send_data);
    } else if (opt == "select") {
        console.log("select...");
        userService.select(send_data);
    } else {
        console.log("no opt...");
    }
});

module.exports = router;

================================================


/**
 * Created by wenshuang3 on 2014/9/11.
 */
var userDao = require('../UserDao');
var UserDaoImpl = function () {
    this.className = "UserDaoImpl";
};
UserDaoImpl.prototype = userDao;
UserDaoImpl.prototype.insert = function (params) {
    params = params || {};
    var callback = params.callback;
    console.log("UserDaoImpl insert...");
    var name = params.name, age = params.age;
    var selectSQL = 'insert into User(name,age) values("' + name + '",' + age + ')';
    var res_obj = {
        title: '用户添加',
        name: name,
        age: age,
        result: [],
        status: false
    };
    this.pool.getConnection(function (err, conn) {
        if (err) console.log("POOL ==> " + err);
        conn.query(selectSQL, function (err, rows) {
            if (err) console.log(err);
            console.log("INSERT ==> ");
            var responseHTML = "";
            conn.release();
            res_obj.result = rows;
            res_obj.status = true;
            callback ? callback(res_obj) : "";
        });
    });
};
UserDaoImpl.prototype.update = function (params) {
    console.log("UserDaoImpl update...");
    return true;
};
UserDaoImpl.prototype.delete = function (params) {
    console.log("UserDaoImpl delete...");
    return true;
};
UserDaoImpl.prototype.select = function (params) {
    params = params || {};
    console.log("UserDaoImpl select...");
    var name = params.name, age = params.age;
    var callback = params.callback;
    var selectSQL = 'select ' + (params.fields ? params.fields : "*") + ' from User' + (params.where ? params.where : "");
    console.log("selectSQL===" + selectSQL);
    var res_obj = {
        title: '用户查询',
        name: name,
        age: age,
        result: [],
        status: false
    };
    this.pool.getConnection(function (err, conn) {
        if (err) console.log("POOL ==> " + err);
        conn.query(selectSQL, function (err, rows) {
            if (err) console.log(err);
            console.log("SELECT ==> ");
            for (var i in rows) {
                console.log(rows[i]);
            }
            res_obj.result = rows;
            res_obj.status = true;
            conn.release();
            callback ? callback(res_obj) : "";
        });
    });
};
module.exports = new UserDaoImpl();

 暂时研究到这,在这只是基础研究,肯定有待深入,望文明吐槽、交流!

你可能感兴趣的:(nodejs,pool)