Nodjs試し(Windows 7)

6047 ワード

もっと読む
会社のプロジェクトを終わらせたばかりです.前に知っていた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();

 ここまでしばらく研究してみますが、これは基礎研究だけです.
  • nodeexp.zip(2.2 MB)
  • ダウンロード回数:0