使用:

// 引入对象
const Koa = require('koa');
// 引入内部方法属性
// const(方法或属性)=require('koa');

// 创建对象
const app = new Koa();


// response
app.use(ctx => {
  ctx.body = 'Hello Koa2';
});

// 端口号  localhost:3000
app.listen(3000);

运行:网址:localhost:3000

 

运行效果:

 

 

 

 

koa-router:

响应:

 

const koa =require('koa');
const app=new koa();
app.use(ctx => {
    const {url} =ctx.request;
    console.log(ctx.request);
    ctx.response.body=url;
    
})
app.listen(3000,()=>{
    console.log('app started on http://localhost:3000');
})

 

 

 

 

  

 

输出格式问题:

const koa =require('koa');
const app=new koa();
app.use(ctx => {
    const {url} =ctx.request;
    console.log(ctx.request);
    // 设定格式    html
    ctx.response.type='html';
    // 插入body标签
    ctx.body='<h1>hello world!</h1>';
    
})
app.listen(3000,()=>{
    console.log('app started on http://localhost:3000');
})

 

 

 
 posted on 2021-11-25 12:16  陶小黑  阅读(38)  评论(0)    收藏  举报