node使用Koa2搭建web项目的方法
随着node.js的日益火热,各种框架开始层出不穷的涌现出来,node.js也开始逐渐的被应用到处理服务端请求的场景中。搭建web项目的框架也随之开始出现——express、koa、koa2、egg等,当然要了解其好坏还是要自己去啃源码的。本文将不会涉及到源码,只是带领初学者简单了解下koa2的基本使用,欢迎大家在评论中互相交流学习。
注意:koa2使用了es7的语法,所以使用时请升级node版本到最新。了解更详细的源码信息可以到去了解
1. 项目目录结构
2. 代码逻辑解析
2.1. 包结构文件
[package.json]
{ "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: ' + 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 + ' |'; reaponsecontent += '年龄:' + user.age + ' |'; 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 }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。