美文网首页
Koa | 0. 安装及初始化

Koa | 0. 安装及初始化

作者: ShadowFieldEric | 来源:发表于2021-01-26 13:44 被阅读0次

    下载模块

    npm install koa
    

    或者,配置文件package.json中配置依赖如下

      "dependencies": {
        "koa": "^2.13.1"
        ...
      }
    

    hello world代码

    // 导入koa,和koa 1.x不同,在koa2中,我们导入的是一个class,因此用大写的Koa表示:
    const Koa = require('koa');
    
    // 创建一个Koa对象表示web app本身:
    const app = new Koa();
    
    // 对于任何请求,app将调用该异步函数处理请求:
    app.use(async (ctx, next) => {
        await next();
        ctx.response.type = 'text/html';
        ctx.response.body = '<h1>Hello, koa2!</h1>';
    });
    
    // 在端口3000监听:
    app.listen(3000);
    console.log('app started at port 3000...');
    

    相关文章

      网友评论

          本文标题:Koa | 0. 安装及初始化

          本文链接:https://www.haomeiwen.com/subject/gyleaktx.html