DEMO-01
- Install koA and KoA-Router
yarn add koa
yarn add koa-router
Copy the code
2. Introduce koA and KoA-Router
const koa = require('koa')
const router = require('koa-router')
Copy the code
3. Configure routes
router.get('/'.(ctx,err) = >{
ctx.body = '<h1>Hello</h1>'
})
// Configure the route
app.use(router.routes()).use(router.allowedMethods())
Copy the code
4. Listen to the port
const port = process.env.PORT || 8005
app.listen(port,_= >{
console.log('Listen to open port 8005')})Copy the code
5. Complete the demo
const Koa = require('koa')
const Router = require('koa-router')
// instantiate koA
const app = new Koa()
const router = new Router()
router.get('/'.(ctx,err) = >{
ctx.body = '<h1>Hello</h1>'
})
// Configure the route
app.use(router.routes()).use(router.allowedMethods())
const port = process.env.PORT || 8005
app.listen(port,_= >{
console.log('Listen to open port 8005')})Copy the code