亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

【備戰春招】第5天 koa

標簽:
Node.js

课程名称:SpringBoot2.X + Vue + UniAPP,全栈开发医疗小程序

课程章节: 第一章

课程讲师:神思者


安装koa

        npm i koa

const Koa = require('koa')
const app = new Koa()
app.use((ctx, next) => {
  console.log('one-1');
  next()
  console.log('one-2');
})
app.use((ctx, next) => {
  console.log('two-1');
  next()
   console.log('two-2');
})
app.use((ctx, next) => {
  console.log('three-1');
  next()
  console.log('three-2');
})
app.listen(3000,()=>{
  console.log('http://127.0.0.1:3000')
})

app.listen 设置端口号


koa 使用得是 洋葱圈模式 

也就是 后进先出

https://img1.sycdn.imooc.com//63e8579b0001bea304970458.jpg


所以上面得代码 得输出

https://img1.sycdn.imooc.com//63e857ad0001a6c603080086.jpg


当使用异步得方式时

const Koa = require('koa')
const app = new Koa()
app.use(async(ctx, next) => {
  console.log('one-1');
  next()
  console.log('one-2');
})
app.use(async(ctx, next) => {
  await console.log('two-1');
  next()
  await console.log('two-2');
})
app.use(async(ctx, next) => {
  console.log('three-1');
  next()
  console.log('three-2');
})
app.listen(3000,()=>{
  console.log('http://127.0.0.1:3000')
})

https://img1.sycdn.imooc.com//63e857cd0001a28302210089.jpg


koa得router插件

npm i @koa/router

router文件夹下 - index.js文件

const Router = require('@koa/router')
const router = new Router({prefix: '/api/v1'})

router.get('/user', ctx=>{
  ctx.body = 'userssss'
})

router.get('/video', ctx=>{
  ctx.body = 'video'
})

module.exports = router

app.js

const Koa = require('koa')
const router = require('./router/index')
const app = new Koa()

app.use(router.routes())

app.listen(3000,()=>{
  console.log('http://127.0.0.1:3000')
})

获得传值信息

router.get('/video/:id', ctx=>{

  ctx.body = 'video'

  console.log(ctx.query)

  console.log(ctx.params)

})


https://img1.sycdn.imooc.com//63e858400001b60110440574.jpg


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
Web前端工程師
手記
粉絲
27
獲贊與收藏
19

關注作者,訂閱最新文章

閱讀免費教程

  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消