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 lang="en"> <head> <meta charset="UTF-8&quo ...

  2. JAVA高并发集合详解

    Queue(队列)主要是为了高并发准备的容器Deque:双端队列,可以反方向装或者取 最开始jdk1.0只有Vector和hashtable 默认所有方法都实现了synchronized锁,线程安全但 ...

  3. CODING 再携手腾讯云 Serverless,让开发者跑步上云

    近年来,腾讯云持续在云原生领域打磨和完善产品矩阵,致力于为开发者上云提供更好的产品和服务.继前段时间 CODING CI 助力腾讯云 Serverless 全新应用控制台.持续保障 Serverles ...

  4. JavaFx ObservableList的使用详解

    原文地址:JavaFx ObservableList的使用详解 | Stars-One的杂货小窝 最近在研究MVVM模式,发现可以将之前写的FxRecyclerView控件改造一下,便是开始尝试,尝试 ...

  5. Compile-time Dependency Injection With Go Cloud's Wire 编译时依赖注入 运行时依赖注入

    Compile-time Dependency Injection With Go Cloud's Wire - The Go Blog https://blog.golang.org/wire Co ...

  6. QTREE----树剖

    题目内容: ---------------------------------------------------- Query on a tree Time Limit: 851MS   Memor ...

  7. java打exe

    参考文章: 注册码: https://www.cnblogs.com/jepson6669/p/9211208.html 官网: https://exe4j.apponic.com/ 在上篇基础上,将 ...

  8. Prometheus自定义监控内容

    Prometheus自定义监控内容 一.io.micrometer的使用 1.1 Counter 1.2 Gauge 1.3 Timer 1.4 Summary 二.扩展 相关内容原文地址: 博客园: ...

  9. Spring MVC参数处理

    使用Servlet API作为参数 HttpServletRequest HttpServletResponse HttpSession 使用流作为参数 总结 Spring MVC通过分析处理处理方法 ...

  10. Centos7 yum 安装 oracle-rdbms-server-11gR2-pre

    Oracleyum官方网站 http://yum.oracle.com/ 一.下载yum源 根据自己需求下载相应的yum源 http://yum.oracle.com/getting-started. ...