Koa-router
先是app.js文件中的代码
const Koa = require('koa')
const router = require('./router/router.js')
const app = new Koa()
app.use(router.routes())
.use(router.allowedMethods())
app.listen(3000, () => {
console.log('start ok')
})
再是router.js文件中的代码
const Router = require('koa-router')
const path = require('path')
const router = new Router()
router
.get('/', (ctx) => {
ctx.body = 'hello koa'
})
.get('/index', (ctx, next) => {
ctx.body = "hello index,这是index哦"
})
module.exports = router
再命令行node app.js 浏览器运行127.0.0.1:3000就行了。
好了,以上就是app.js和router.js文件的抽取分离。