9-廖雪峰-node-web-koa2-url

作者: _panda | 来源:发表于2017-08-09 16:32 被阅读112次

koa-router

在hello-koa工程中,我们处理http请求一律返回相同的HTML,这样虽然非常简单,但是用浏览器一测,随便输入任何URL都会返回相同网页。

正常情况下,我们应该对不同的URL调用不同的处理函数,这样才能返回不同的结果。例如像这样写:

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();
    }
});

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

这么写是可以运行的,但是好像有点蠢。

应该有一个能集中处理URL的middleware,它根据不同的URL调用不同的处理函数,这样,我们才能专心为每个URL编写处理函数。

==koa-router==

为了处理URL,我们需要引入koa-router这个middleware,让它负责处理URL映射。

我们把上一节的hello-koa工程复制一份,重命名为url-koa。

先在package.json中添加依赖项:

"koa-router": "7.0.0"
//然后用npm install安装。

接下来,我们修改app.js,使用koa-router来处理URL:

const Koa = require('koa');

// 注意require('koa-router')返回的是函数:
const router = require('koa-router')();

const app = new Koa();

// log request URL:
app.use(async (ctx, next) => {
    console.log(`Process ${ctx.request.method} ${ctx.request.url}...`);
    await next();
});

// add url-route:
router.get('/hello/:name', async (ctx, next) => {
    var name = ctx.params.name;
    ctx.response.body = `<h1>Hello, ${name}!</h1>`;
});

router.get('/', async (ctx, next) => {
    ctx.response.body = '<h1>Index</h1>';
});

// add router middleware:
app.use(router.routes());

app.listen(3000);
console.log('app started at port 3000...');

注意导入koa-router的语句最后的()是函数调用:

const router = require('koa-router')();
相当于:

const fn_router = require('koa-router');
const router = fn_router();

相关文章

  • 9-廖雪峰-node-web-koa2-url

    koa-router 在hello-koa工程中,我们处理http请求一律返回相同的HTML,这样虽然非常简单,但...

  • 廖雪峰

    https://www.liaoxuefeng.com/wiki/0014316089557264a6b34895...

  • 廖雪峰

  • 如何用git将代码上传到码云

    安装Git 建议直接浏览廖雪峰老师的安装介绍廖雪峰老师的安装Git教程 如果你想系统的了解Git的话,廖雪峰老师的...

  • 将自己的项目放到码云上托管

    安装Git 建议直接浏览廖雪峰老师的安装介绍廖雪峰老师的安装Git教程 如果你想系统的了解Git的话,廖雪峰老师的...

  • 安装Git

    建议直接浏览廖雪峰老师的安装介绍廖雪峰老师的安装Git教程 如果你想系统的了解Git的话,廖雪峰老师的这个网站是个...

  • 廖雪峰git

    廖雪峰git

  • Git版本控制学习资料

    一、个人感觉写的非常好,廖雪峰的git教程: 廖雪峰git教程

  • python学习

    使用的教材:廖雪峰教材 另外附一个廖雪峰笔记,感觉写的比我简练,可以用来查询具体用法:廖雪峰笔记 更多的可以查询:...

  • git分布式开发

    ## test ## 廖雪峰Git

网友评论

    本文标题:9-廖雪峰-node-web-koa2-url

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