前几天想写个小爬虫程序,准备后端就用koa2。因而翻遍github与各大网站,都没找到一个好用的、轻一点的koa2脚手架,也找不到一个清晰些的搭建介绍。github上的脚手架要么是1.x版的koa,要么一堆复杂的依赖。javascript
固然可能仍是有写的比较好的吧,只是我没找到。无论怎样吧,我只能亲自上了,就当是学习了。前端
如今把搭建过程介绍下,看能不能方便下入门的同窗。java
官方的介绍,是很简单的。node
$ npm install koa
const Koa = require('koa') const app = new Koa() // response app.use(ctx => { ctx.body = 'Hello Koa' }) app.listen(3000)
好,那咱们就先从这开始。建立一个文件夹,命名koa2。(记得先装好node v7.6.0 以上版本)webpack
cd koa2 npm init // 一路回车,根据提示输入信息。 npm install koa --save
而后在文件下根目录下建立程序入口文件:index.js,并把官网介绍那段代码贴进去。以后在命令行中执行git
node index.js
打开浏览器,访问 http://localhost:3000/
,能够看到页面输出了 hello world
。es6
很好,第一步已经踏出去了。相信到这里大部分小白都没问题,以后就开始懵逼了。就这个玩意,我该怎么写接口?怎么链接数据库?github
Koa本质上是调用一系列的中间件,来处理对应的请求,并决定是否传递到下一个中间件去处理。咱们来写一个最简单的中间件试试。web
// 刚才index.js 中的这段代码,咱们改写一下。 app.use(ctx => { ctx.body = 'Hello Koa' }) // 改为以下 app.use(ctx => { ctx.body = `您的网址路径为:${ctx.request.url}` })
这段代码中,app.use
的 function
就是最简单的一个中间件,接受了请求,读出请求路径,并返回到客户端。从新执行下node index.js
,打开浏览器,输入http://localhost:3000/hhhhh
,页面输出了您的网址路径为:hhhhh
。数据库
因此,接口的本质,就是判断不一样的请求连接,干不一样的事情,返回相应的结果。那么咱们得须要一个路由中间件来处理分发请求。开源的时代,固然是拿来主义了。github搜下koa-router,成功找到。根据它的介绍,咱们先在项目根目录执行
npm install koa-router --save
而后把index.js
文件再改造下。变成以下:
const Koa = require('koa') const Router = require('koa-router') const app = new Koa() const router = new Router() // 先注释了,后面再解释 // const bodyParser = require('koa-bodyparser') // app.use(bodyParser()) router.get('/', ctx => { ctx.body = `这是主页` }) router.get('/user', ctx => { ctx.body = `这是user页` }) router.get('/post', ctx => { ctx.body = ctx.request.body }) router.get('/async', async ctx => { const sleep = async (ms) => { return new Promise(resolve => { setTimeout(() => { resolve(true) }, ms) }) } await sleep(1000) ctx.body = `这是异步处理页` }) app .use(router.routes()) .use(router.allowedMethods()) app.listen(3000)
从新执行 node index.js
。咱们能够看到访问 /
, /user
,/async
,都能获得相应的结果了。
除了那个post的方法,压根得不到本身post的数据。
由于koa是很纯粹的,你提交的数据,它并不会帮你处理。因此这里咱们又必须引用一个中间件来处理提交的数据--bodyparser。把上面那两行注释代码解注,就能处理请求数据了。记得要先
npm install koa-bodyparser --save
另外关于async/await
不明白的同窗,能够先去看下阮老师的介绍,点击传送门。
不过咱们不能把全部的接口都写在这一个文件呀,因此咱们得改造下。理一下思路,路由的配置文件应该单独一份,接口的方法应该按业务模块分红一个个controller。说干就干!
先看改造后的目录结构,不想截图,你们将就看看:
-koa2 -node_modules -controller user.js -index.js -router.js -package.json
再来看文件变成怎么样了。
// index.js const Koa = require('koa') const app = new Koa() const router = require('./router') const bodyParser = require('koa-bodyparser') app.use(bodyParser()) app .use(router.routes()) .use(router.allowedMethods()) app.listen(3000)
// router.js const Router = require('koa-router') const router = new Router() const user = require('./controller/user') router.post('/user/login', user.login) router.get('/user/profile', user.profile) module.exports = router
// controller/user.js const sleep = async (ms) => { return new Promise(resolve => { setTimeout(() => { resolve(true) }, ms) }) } module.exports = { login (ctx) { ctx.body = { username: ctx.request.body.username } }, async profile (ctx) { await sleep(1000) ctx.body = { username: '相学长', sex: 'man', age: '999' } } }
再从新执行 node index.js
。访问相应路由,应该能获得对应的结果了。
好,到此为止,咱们的server已经大体完成了,可是咱们发现一个很烦的问题就是,每次修改代码都得从新node index.js
,这也太烦了。我但愿的是,每次更新代码都能从新执行,而且帮我执行ESlint。其余前端项目webpack那一套,不是webpack配置工程师的话,本身挪过来又改不来。
这里我介绍个简单的方案,nodemon + gulp
。具体呢就不一步步来了,这种东西,不用太了解,能run起来知足本身需求就好。若是不须要eslint的话,只要安装nodemon就好。
package.json scripts部分 修改成:
"scripts": { "nodemon": "nodemon index.js" }
而后命令行执行:
npm install nodemon --save-dev npm run nodemon
若是有eslint的需求的话,就稍微麻烦些了,eslint的init我就不贴教程了,我贴上个人gulp配置文件:
// gulpfile.js const gulp = require('gulp') const lint = require('gulp-eslint') const nodemon = require('gulp-nodemon') function lintFiles (files) { return gulp.src(files) .pipe(lint()) .pipe(lint.format()) // .pipe(lint.failAfterError()) } gulp.task('eslint', () => lintFiles(['**/*.js', '!node_modules/**'])) gulp.task('eslint_nodemon', ['eslint'], () => { return nodemon({ script: './app/server.js', // 项目入口文件 tasks (changedFiles) { lintFiles(changedFiles) return [] }, ignore: ['build/**', 'dist/**', '.git', 'node_modules/**'] }) }) gulp.task('default', ['eslint_nodemon'])
// package.json scripts "scripts": { "start": "pm2 start index.js --watch", // 这里用pm2 做为线上run,有兴趣的同窗能够本身去看看 "dev": "gulp", "lint": "eslint .", "fix": "eslint --fix ." },
到这里,我想应该能让一部分同窗上手了。
但这只是初步的搭建了下koa。真的想投入使用,根据业务需求,可能还须要安装数据库驱动等中间件。对于复杂业务场景的server,还须要更加合理的设计controller,service,在这里就很少阐述了。
若是这篇文章,可以帮助到一些同窗,下次有空再写写这方面相关的。
ps: 文章介绍的全部代码都传了一份到github,有须要的同窗请自行去看。