1. 程式人生 > >使用Koa2搭建web項目

使用Koa2搭建web項目

gist info etc 添加 處理方法 啟動 substr rip 前端

  隨著Node.js的日益火熱,各種框架開始層出不窮的湧現出來,Node.js也開始逐漸的被應用到處理服務端請求的場景中。搭建Web項目的框架也隨之開始出現——express、koa、koa2、egg等,當然要了解其好壞還是要自己去啃源碼的。本文將不會涉及到源碼,只是帶領初學者簡單了解下Koa2的基本使用,歡迎大家在評論中互相交流學習。
註意:koa2使用了ES7的語法,所以使用時請升級Node版本到最新。 

  了解更詳細的源碼信息可以到git上的koajs/koa去了解。

1.項目目錄結構

技術分享圖片

2. 代碼邏輯解析

2.1. 包結構文件

{
  "name": "weixin-node-koa",
  
"version": "1.0.0", "description": "node.js with koa2", "private": true, "dependencies": { "koa": "^2.0.0", "koa-router": "^7.0.0", "mysql":"2.13.0" }, "scripts": { "start": "node app.js" }, "engines": { "node": ">=6.0.0" }, "author": "Fly", "license": "CENTERM" }

2.2. 啟動入口文件

app.js

const Koa = require(‘koa‘);
const app = new Koa();
const router2controller = require(‘./app/router2controller.js‘);
const config = require(‘./config/config.local.js‘);

app.use(router2controller());
app.listen(config.port);
console.log("Server started and listen on port " + config.port);

如果請求的報文體是XML格式,可以添加下面的代碼自動解析報文(註意引用koa-xxx的版本要與koa2對應)

const Koa = require(‘koa‘);
const app = new Koa();
const router2controller = require(‘./app/router2controller.js‘);
const config = require(‘./config/config.local.js‘);

//start接收到的xml數據請求單獨解析存儲
const xmlParser = require(‘koa-xml-body‘);
app.use(xmlParser()).use((ctx,next) => {
    ctx.data = ctx.request.body;
    return next();
});
//end

app.use(router2controller());
app.listen(config.port);
console.log("Server started and listen on port " + config.port);

從代碼看到引入了一個router2controller.js的文件,這個文件是完成前端請求到具體處理方法的路由過程

2.3. 路由器文件

router2controller.js,該類將會自動掃描controller文件夾中的文件來加載請求映射,不需要挨個請求單獨配置

koa-router原生提供方法如下:

router
.get(‘/‘, async (ctx,next) => {
this.body = ‘Hello World!‘;
})
.post(‘/users‘, async (ctx,next) => {
//TODO
})
.put(‘/users/:id‘, async (ctx,next) => {
//TODO
})
.del(‘/users/:id‘, async (ctx,next) => {
//TODO
});

自動掃描controller包實現方法如下

const fs = require(‘fs‘);
const router = require(‘koa-router‘)();

function addMapping(router, mapping) {
    for (var url in mapping) {
        if (url.startsWith(‘GET ‘)) {
            var path = url.substring(4);
            router.get(path, mapping[url]);
            console.log(`register URL mapping: GET ${path}`);
        } else if (url.startsWith(‘POST ‘)) {
            var path = url.substring(5);
            router.post(path, mapping[url]);
            console.log(`register URL mapping: POST ${path}`);
        } else if (url.startsWith(‘PUT ‘)) {
            var path = url.substring(4);
            router.put(path, mapping[url]);
            console.log(`register URL mapping: PUT ${path}`);
        } else if (url.startsWith(‘DELETE ‘)) {
            var path = url.substring(7);
            router.del(path, mapping[url]);
            console.log(`register URL mapping: DELETE ${path}`);
        } else {
            console.log(`invalid URL: ${url}`);
        }
    }
}

function addControllers(router, dir) {
    fs.readdirSync(__dirname + ‘/‘ + dir).filter((f) => {
        return f.endsWith(‘.js‘);
    }).forEach((f) => {
        console.log(`process controller: ${f}...`);
        let mapping = require(__dirname + ‘/‘ + dir + ‘/‘ + f);
        addMapping(router, mapping);
    });
}

module.exports = function (dir) {
    var controllersDir = dir || ‘controller‘;
    addControllers(router, controllersDir);
    return router.routes();
};

2.4. 控制器

userController.js,***Controller.js是用來處理具體請求信息以及返回數據的,userController.js中處理了GET請求獲取用戶信息,POST請求保存用戶信息

const userService = require(‘./../service/userService.js‘);

var getUserinfo = (ctx, next) => {
    let query = ctx.query;
    let userId = query.id;
    let userInfo = userService.getUserById(userId);

    let html = ‘<html><body>‘
        + ‘<div> userinfo:&nbsp;‘ + userInfo + ‘</div>‘
        + ‘</body></html>‘;
    ctx.response.type =‘text/html‘;
    ctx.response.body = html;
};

var saveUserinfo = (ctx, next) => {
    const requestString = ctx.data;
    //TODO數據處理
    Console.log(requestString);
};

module.exports = {
    ‘GET /getUserinfo‘: getUserinfo,
    ‘POST /saveUserinfo‘: saveUserinfo
};

2.5. 數據處理

userService.js,處理封裝從***Dao.js獲取到的數據返回給Controller

const userDao = require(‘./../dao/userDao.js‘);

var getUserById = async (userId) => {
    var users = userDao.getUserById(userId);
    var responseContent = ‘‘;
    for(let user of users) {
        reaponseContent += ‘姓名:‘ + user.name + ‘&nbsp;|‘;
        reaponseContent += ‘年齡:‘ + user.age + ‘&nbsp;|‘;
        reaponseContent += ‘身高:‘ + user.height + ‘<br />‘;
    }
    return responseContent;
}

module.exports = {
    getUserById : getUserById
};

2.6. 數據獲取

userDao.js,通過請求傳入參數來獲取user數據

const mysql = require(‘./../utils/mysqlUtil.js‘);

var getUserById = async (userId) => {
    let mysqlOptions = {
        sql : ‘select * from table_user where user_id = ?‘,
        args : [userId]
    };

    var users = await mysql.execQuery(mysqlOptions);
    if(users.length == 0) {
        return null;
    } else {
        return users;
    }
};

module.exports = {
    getUserById : getUserById
};

2.7. 數據庫操作

mysqlUtil.js,包含了數據庫連接池控制,連接建立、釋放管理,執行Dao發起的數據庫操作請求

const mysql = require(‘mysql‘);
const config = require(‘./../../config/config.local.js‘);

var connectionPool = mysql.createPool({
    ‘host‘ : config.database.host,
    ‘port‘:config.database.port,
    ‘user‘ : config.database.user,
    ‘password‘ : config.database.password,
    ‘database‘ : config.database.database,
    ‘charset‘: config.database.charset,
    ‘connectionLimit‘: config.database.connectionLimit,
    ‘supportBigNumbers‘: true,
    ‘bigNumberStrings‘: true
});

var release = connection => {
    connection.end(function(error) {
        if(error) {
            console.log(‘Connection closed failed.‘);
        } else {
            console.log(‘Connection closed succeeded.‘);
        }
    });
};

var execQuery = sqlOptions => {
    var results = new Promise((resolve, reject) => {
            connectionPool.getConnection((error,connection) => {
            if(error) {
                console.log("Get connection from mysql pool failed !");
                throw error;
            }

            var sql = sqlOptions[‘sql‘];
            var args = sqlOptions[‘args‘];

            if(!args) {
                var query = connection.query(sql, (error, results) => {
                    if(error) {
                        console.log(‘Execute query error !‘);
                        throw error;
                    }

                    resolve(results);
                });
            } else {
                var query = connection.query(sql, args, function(error, results) {
                    if(error) {
                        console.log(‘Execute query error !‘);
                        throw error;
                    }

                    resolve(results);
                });
            }

            connection.release(function(error) {
                if(error) {
                    console.log(‘Mysql connection close failed !‘);
                    throw error;
                }
            });
        });
    }).then(function (chunk) {
        return chunk;
    });

    return results;
};

module.exports = {
    release : release,
    execQuery : execQuery
}

此實例我並沒有整理並在本機運行過,但這個包結構的思路還是值得學習的,整個實例的代碼詳見http://bijian1013.iteye.com/blog/2425085。

文章來源:https://blog.csdn.net/ererfei/article/details/68060551

使用Koa2搭建web項目