koa url path & koa-router

koa url path & koa-router

url path & regex

koa path router

"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2020-09-01
 * @modified
 *
 * @description
 * @difficulty Easy Medium Hard
 * @complexity O(n)
 * @augments
 * @example
 * @link
 * @solutions
 *
 * @best_solutions
 *
 */

const log = console.log;

const fs = require('fs');

const Koa = require('koa');
const app = new Koa();

// const main = ctx => {
//   ctx.response.body = 'Hello World';
// };

const main = ctx => {
  // log(`ctx.request`, ctx.request);
  // log(`ctx.request.accepts`, ctx.request.accepts);
  // ctx.request.accepts [Function: accepts]
  // if (ctx.request.accepts('xml')) {
  //   ctx.response.type = 'xml';
  //   ctx.response.body = '<data>Hello World</data>';
  // } else if (ctx.request.accepts('json')) {
  //   ctx.response.type = 'json';
  //   ctx.response.body = { data: 'Hello World' };
  // } else if (ctx.request.accepts('html')) {
  //   ctx.response.type = 'html';
  //   ctx.response.body = '<p>Hello World</p>';
  // } else {
  //   ctx.response.type = 'text';
  //   ctx.response.body = 'Hello World';
  // }
  // ctx.response.type = 'json';
  // ctx.response.body = { data: 'Hello World', };
  // SSR
  // 相對路徑 vs 就對路徑 ✅
  // const path = './src/templates/template.html';
  // ctx.response.type = 'html';
  // ctx.response.body = fs.createReadStream(path);
  /*
    http://localhost:3000/app/index.html
    /app/index.html 200 ✅
    http://localhost:3000/app/index.php
    /app/index.php 404 ❌
  */
  log(`ctx.request.path`, ctx.request.path);
  const path = ctx.request.path;
  // let template = './src/templates/template.html';
  let template = '';
  switch (path) {
    case "index/*":
      template = './src/templates/index.html';
      break;
    // URL path match regex ❓
    case "test/**.html":
      template = './src/templates/test.html';
      break;
    case "404/*":
    default:
      template = './src/templates/404.html';
      break;
  }
  // ctx.response.type = 'json';
  ctx.response.type = 'html';
  // ctx.response.body = '<p>Hello World</p>';
  ctx.response.body = fs.createReadStream(template);
};

app.use(main);
log(`koa app running on http://127.0.0.1:3000`);
log(`koa app running on http://localhost:3000`);
app.listen(3000);


koa-router

$ yarn add koa-router 
# OR
$ npm i koa-router

https://github.com/ZijianHe/koa-router

router
  .get('/', (ctx, next) => {
    ctx.body = 'Hello World!';
  })
  .post('/users', (ctx, next) => {
    // ...
  })
  .put('/users/:id', (ctx, next) => {
    // ...
  })
  .del('/users/:id', (ctx, next) => {
    // ...
  })
  .all('/users/:id', (ctx, next) => {
    // ...
  });

var url = Router.url('/users/:id', {id: 1});
// => "/users/1"

const url = Router.url('/users/:id', {id: 1}, {query: { active: true }});
// => "/users/1?active=true"

Error: Cannot find module 'koa-router'

koa-router !== koa-route

# ✅ install OK 👍
$ yarn add koa-router   

https://www.npmjs.com/package/koa-router

# ❌ install by mistake 👎
$ yarn add koa-route

https://www.npmjs.com/package/koa-route

https://github.com/ZijianHe/koa-router/issues/528

refs

http://www.ruanyifeng.com/blog/2017/08/koa.html

https://github.com/koajs/path-match

https://stackoverflow.com/questions/39388287/get-request-parameters-with-koa-router



©xgqfrms 2012-2020

www.cnblogs.com 發佈文章使用:只允許註冊用戶纔可以訪問!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章