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 发布文章使用:只允许注册用户才可以访问!


koa url path & koa-router的更多相关文章

  1. Django web框架-----url path name详解

    说明:mytestsite是django框架下的项目,quicktool是mytestsite项目中的应用 quicktool/view.py文件修改视图函数index(),渲染一个home.html ...

  2. Koa框架教程,Koa框架开发指南,Koa框架中文使用手册,Koa框架中文文档

    我的博客:CODE大全:www.codedq.net:业余草:www.xttblog.com:爱分享:www.ndislwf.com或ifxvn.com. Koa -- 基于 Node.js 平台的下 ...

  3. Redis 存储图片 [base64/url/path]vs[object]

    一.base64图片编解码 基本流程:从网络获取下载一张图片.然后base64编码,再base64解码,存到本地E盘根文件夹下. import java.awt.image.BufferedImage ...

  4. this.getClass().getResource("") url path file 区别

    首先注意 "/word/appointDismiss.docx" 前面一定要加 /,有一次我就是忘记加/ 查了半天错, 不能写成 "word/appointDismiss ...

  5. Nodejs file path to url path

    import * as path from 'path'; import * as url from 'url'; const savePath = path.join('public', 'imag ...

  6. 跟我一起了解koa之在koa中使用redis

    第一步安装中间件 cnpm i koa-generic-session koa-redis 第二步引入中间件 在中间件中写入session 浏览器中会存储数据 第三步关于Redis来读取和存储数据 读 ...

  7. 跟我一起学koa之在koa中使用mongoose(四)

    第一步安装mongoose,创建数据库文件夹 第二步引入mongoose,连接数据库 第三步运行项目 这个报错 只需要将es6写法变成es5写法即可 我们连接数据库,并且以post请求的方式插入数据 ...

  8. gin中的重定向

    package main import ( "github.com/gin-gonic/gin" ) func main() { // HTTP重定向很容易,内部.外部重定向均支持 ...

  9. KoaHub平台基于Node.js开发的Koa router路由插件代码信息详情

    koa-router Router middleware for koa. Provides RESTful resource routing. koa-router       Router mid ...

随机推荐

  1. jQuery 留言表单验证

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. Soul API 网关源码解析 03

    目标 使用 soul 代理 dubbo 服务 dubbo 服务如何注册到网关的? dubbo 插件是如何工作的? 理清 http --> 网关--> dubbo provider 整条链路 ...

  3. 用git合并分支时,如何保持某些文件不被合并

    用git合并分支时,如何保持某些文件不被合并_fkaking的专栏-CSDN博客_git 合并分支 https://blog.csdn.net/fkaking/article/details/4495 ...

  4. 思考gRPC :为什么是HTTP/2

    Introducing gRPC Support with NGINX 1.13.10 - NGINX https://www.nginx.com/blog/nginx-1-13-10-grpc/ 思 ...

  5. tcpdump 参数详解及使用案例

    参数 -A 以ASCII码方式显示每一个数据包(不会显示数据包中链路层头部信息). 在抓取包含网页数据的数据包时, 可方便查看数据(nt: 即Handy for capturing web pages ...

  6. 用tqdm和rich为固定路径和目标的python算法代码实现进度条

    适用场景 在存在固定长度的算法中可以可视化算法执行的过程,比如对一个固定长度的数组的遍历,就是一种适合使用进度条来进行可视化的场景.而一些条件循环,比如while循环,不一定适合使用进度条来对算法执行 ...

  7. 20201102gryz模拟赛解题报告

    简述我的苦逼做题经历 考的是NOIP2017day1原题, 开始看到小凯的疑惑时感觉特水,因为这题初中老师讲过, 很nice的秒切 T2发现是个大模拟,虽然字符串不太会用,但起码题意很好理解 边打代码 ...

  8. loj10018数的划分

    题目描述 将整数 n 分成 k 份,且每份不能为空,问有多少种不同的分法.当 n=7,k=3 时,下面三种分法被认为是相同的:1 1 5;1 5 1 ;5 1 1 输入格式 一行两个数 n ,k . ...

  9. jQuery——通过Ajax发送数据

    Ajax(Asynchronous JavaScript and XML,异步JavaScript和XML),一个Ajax解决方案涉及如下技术: JavaScript:处理与用户及其他浏览器相关事件的 ...

  10. Spring boot freemarker 配置

    spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driverClassName: com.mysql.jdbc.Dri ...