遇见koa


koa2

koa 是 Express 的下一代基于 Node.js 的 web 框架,koa2 基于 ES7 开发,完全的使用 promise 配合 async 实现异步

基本结构

const koa = require("koa");

const app = new koa();

app.use(async (ctx, next) => {
    await next();
    ctx.response.type = "text/html";
    ctx.response.body = "<h1>hello koa2</h1>";
});
const port = 3000;
app.listen(port, () => {
    console.log(`app started at port ${port}`);
});

那么处理不同的路径就可以写if(){}else{}或者多个 app.use,就像这样

app.use(async (ctx, next) => {
    if (ctx.request.path === "/") {
        ctx.response.body = "index page";
    } else {
        await next();
    }
});

app.use(async (ctx, next) => {
    if (ctx.request.path === "/test") {
        ctx.response.body = "TEST page";
    } else {
        await next();
    }
});

这就写法又太啰嗦了,这时候就可以引入中间件来集中处理这些 url

中间件

koa-router

基本的结构

const koa = require("koa");
const Router = require("koa-router");
const app = new koa();
const router = new Router();
router
    .get("/", async (ctx, next) => {
        ctx.body = "koa-router";
    })
    .get("/js", async (ctx, next) => {
        ctx.body = "js";
    });

app.use(router.routes()).use(router.allowedMethods());
const port = 3000;
app.listen(port, () => {
    console.log(`app started at port ${port}`);
});

路由的层级

const koa = require("koa");
const Router = require("koa-router");
const app = new koa();

let home1 = new Router();
home1
    .get("/js1", async (ctx, next) => {
        ctx.body = "js1";
    })
    .get("/js2", async (ctx, next) => {
        ctx.body = "js2";
    });

let home2 = new Router();
home2
    .get("/koa", async (ctx, next) => {
        ctx.body = "koa";
    })
    .get("/koa2", async (ctx, next) => {
        ctx.body = "koa2";
    });

let router = new Router();
router.use("/home1", home1.routes(), home1.allowedMethods());
router.use("/home2", home2.routes(), home2.allowedMethods());
app.use(router.routes()).use(router.allowedMethods());
const port = 3000;
app.listen(port, () => {
    console.log(`app started at port ${port}`);
});

koa-bodyparser

解析原始 request 请求

const koa = require("koa");
const app = new koa();
const bodyParse = require("koa-bodyparser");
app.use(bodyParse());
app.use(async (ctx, next) => {
    if (ctx.url === "/" && ctx.method === "GET") {
        let html = `
            <form method="POST" action="/">
                <p>userName</p>
                <input name="userName" /><br/>
                <p>age</p>
                <input name="age" /><br/>
                <p>website</p>
                <input name="webSite" /><br/>
                <button type="submit">submit</button>
            </form>
        `;
        ctx.body = html;
    } else if (ctx.url === "/" && ctx.method === "POST") {
        ctx.body = ctx.request.body;
    } else {
        ctx.body = "404";
        await next();
    }
});
const port = 3000;
app.listen(port, () => {
    console.log(`app started at port ${port}`);
});

模板引擎

使用 ejs 和 koa-views
在根目录新建 view 文件夹,写一个 hello.ejs 文件

const koa = require("koa");
const views = require("koa-views");
const path = require("path");
const app = new koa();

app.use(
    views(path.join(__dirname, "./view"), {
        extension: "ejs"
    })
);

app.use(async (ctx, next) => {
    let title = "hello";
    await ctx.render("hello", {
        title
    });
});
const port = 3000;
app.listen(port, () => {
    console.log(`app started at port ${port}`);
});

如果需要使用静态资源则需要引入 koa-static,然后就可以直接用资源名访问

const koa = require("koa");
const static = require("koa-static");
const path = require("path");
const app = new koa();

const staticPath = "./static";
app.use(static(path.join(__dirname, staticPath)));
app.use(async ctx => {
    ctx.body = "hello world";
});
const port = 3000;
app.listen(port, () => {
    console.log(`app started at port ${port}`);
});

文章作者: 沐雪
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 沐雪 !
评论
 上一篇
Nginx是什么 Nginx是什么
Nginx作为一个前端可能没用过 Nginx,但绝对听过,那么他到底是什么呢 Nginx 是一款自由的、开源的、高性能的 HTTP 服务器和反向代理服务器;同时也是一个 IMAP、POP3、SMTP 代理服务器;Nginx 可以作为一个 H
2019-01-16 沐雪
下一篇 
不如学MongoDB 不如学MongoDB
mongodb安装配置下载就不说了,说说配置把下载的文件放到你想放到的目录(比如:/usr/local)添加环境变量 export PATH=${PATH}:/usr/local/MongoDB/bin无论是 zsh 还是 bash 编辑完
2018-12-10 沐雪
  目录