express(四):node js(express 模板)+MySQL

话不多说先放几张图,首页的 css 是借鉴的,核心代码已经贴出.


express(四):node js(express 模板)+MySQL_第1张图片
代码结构

访问地址:http://123.206.30.14:3521 ,当你看到这个地址时候,估计已经被 Tencent 回收了服务器空间.


express(四):node js(express 模板)+MySQL_第2张图片
Home 首页
selectAllRows: function (cb) {
        pool.getConnection(function (error, connection) {
            if (error) {
                console.log('获取数据库连接失败');
                throw error;
                // return;
            }
            connection.query(sqlList.selectAllRowsData, function (error, result) {
                if (error) {
                    console.log('查询失败');
                    throw error;
                    // return '查询失败';
                }
                if (result) {
                    try {
                        //result = JSON.stringify(result);
                    } catch (e) {
                        if (e) {
                            console.log(e.stack);
                        }
                    }
                    cb(error, result);
                }
                connection.release();
            });
        });
    }
express(四):node js(express 模板)+MySQL_第3张图片
List 页面
selectAllRows: function (cb) {
        pool.getConnection(function (error, connection) {
            if (error) {
                console.log('获取数据库连接失败');
                throw error;
                // return;
            }
            connection.query(sqlList.selectAllRowsData, function (error, result) {
                if (error) {
                    console.log('查询失败');
                    throw error;
                    // return '查询失败';
                }
                if (result) {
                    try {
                        //result = JSON.stringify(result);
                    } catch (e) {
                        if (e) {
                            console.log(e.stack);
                        }
                    }
                    cb(error, result);
                }
                connection.release();
            });
        });
    }
express(四):node js(express 模板)+MySQL_第4张图片
Update 页面
updateDataForAdmin: function (req, res, next, cb) {
        pool.getConnection(function (error, connection) {
            if (error) {
                console.log('获取数据库连接失败');
                throw  error;
                // return;
            }
            //获取请求参数
            var param = req.body || req.params;
            var date = new Date();
            // 'update readinglist set title = ?, subtitle = ?, iconUrl = ?, editTime = ? where  rid = ?'
            connection.query(sqlList.updateData, [param.editTitle, param.editSubtitle, param.editIconUrl,date,param.rid], function (error, result) {
                console.log('插入开始');
                if (error) {
                    console.log('新增失败');
                    throw error;
                    // return;
                }
                // assert.ifError(error);
                if (result) {
                  cb(req, res, error, result);
                }
                //formatData(res, result);
                connection.release();
            });
        });
    }
express(四):node js(express 模板)+MySQL_第5张图片
Delete 页面
deleteDataForAdmin: function (req, res, next, cb) {
        pool.getConnection(function (error, connection) {
            if (error) {
                throw  error;
                console.log('获取数据库连接失败');
                //return;
            }
            //获取请求参数
            var param = req.param('rid');
            console.log('参数为:' + param);
            var comma = ',';
            if (param.indexOf(comma) < 0) {
                console.log('删除多个');//删除单个
                connection.query(sqlList.deleteData, param, function (error, result) {
                    console.log('删除开始');
                    if (error) {
                        throw  error;
                        console.log('删除失败', error);
                        //return;
                    }
                    if (result) {
                          cb(error, result);
                    }
                    connection.release();
                });
            }
            else {
                console.log('删除多个');//删除多个
                var itemsCount = 0;
                var items = param.split(',');
                for (var i = 0; i < items.length; i++) {
                    //console.log('参数是一个数组' +param.length + "-----" + param[i].rid);
                    connection.query(sqlList.deleteData, items[i], function (error, result) {
                        console.log('删除开始' + i);
                        if (error) {
                            throw error;
                            console.log('删除失败', error);
                            //return;
                        }
                        if (result) {
                            itemsCount++;
                            console.log(itemsCount + '==========');
                            if (itemsCount == items.length) {
                                console.log('循环' + i);
                                cb(req, res, error, result);
                                connection.release();
                            }
                        }
                    });
                }
            }
        });
    }

配置的路由 js 如下:

var express = require('express');
var router = express.Router();

var dbDao = require('../db/DBDao');
var async = require('async');

/* GET home page. */
router.get('/', function (req, res, next) {
    res.render('index', {title: 'Express'});
});

router.get('/showDetail', function (req, res) {

    dbDao.selectAllRows(function (error, result) {

        if (result) {
            console.log('回调结果' + result);

            res.render('detail', {
                dataRows: result,
                title: req.param('pagetype')
            });
        } else {
            res.render('error', {});
        }

    });

});

router.post('/addData', function (req, res, next){

    dbDao.insertDataForAdmin(req, res , next ,function(error, result){

        if (result){
            console.log('插入成功');
            //重新查询,渲染页面
            dbDao.selectAllRows(function (error, result) {

                if (result) {
                    console.log('回调结果' + result);

                    res.render('detail', {

                        dataRows: result,
                        title: req.param('pagetype')
                    });
                } else {
                    res.render('error', {});
                }

            });

        }

    });


});

router.get('/deleteData',function(req, res, next){

    dbDao.deleteDataForAdmin(req, res , next ,function(error, result){

        if (result){
            //console.log('删除成功');
            //重新查询,渲染页面
            dbDao.selectAllRows(function (error, result) {

                if (result) {
                    console.log('回调结果' + result);

                    res.render('detail', {
                        dataRows: result,
                        title: req.param('pagetype')
                    });
                } else {
                    res.render('error', {});
                }

            });

        }

    });
});

router.post('/updateData',function(req, res, next){

    dbDao.updateDataForAdmin(req, res , next ,function(error, result){

        if (result){

            //重新查询,渲染页面
            dbDao.selectAllRows(function (error, result) {

                if (result) {
                    console.log('回调结果' + result);

                    res.render('detail', {
                        dataRows: result,
                        title: req.param('pagetype')
                    });
                } else {
                    res.render('error', {});
                }

            });

        }

    });
});

module.exports = router;

你可能感兴趣的:(express(四):node js(express 模板)+MySQL)