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. Java Optional使用指南

    提到NullPointerException(简称NPE)异常,相信每个Java开发人员都不陌生,从接触编程的第1天起,它就和我们如影随形,最近处理的线上bug中,有不少都是对象没判空导致的NullP ...

  2. 将连续增长 N 次字符串所需的内存重分配次数从必定 N 次降低为最多 N 次 二进制安全

    SDS 与 C 字符串的区别 - Redis 设计与实现 http://redisbook.com/preview/sds/different_between_sds_and_c_string.htm ...

  3. Win+R 快速启动程序

    将某个程序的可执行C:\Program Files\Oracle\VirtualBox\VirtualBox.exe 或其快捷键 放入 某个自建的快捷键集合文件夹,可以自定义命名 如 vb 将其添加到 ...

  4. 更新gitignore后如何使其生效

    Files already tracked by Git are not affected; Git - gitignore Documentation https://git-scm.com/doc ...

  5. python基础(int,str,bool,list)

    1数字int. 数字主要是用于计算用的,使用方法并不是很多,就记住一种就可以:      bit_length() #bit_length() 当十进制用二进制表示时,最少使用的位数 v = 11 1 ...

  6. LOJ10163 Amount of Degrees

    题目描述 求给定区间 [X,Y] 中满足下列条件的整数个数:这个数恰好等于 KK 个互不相等的 BB 的整数次幂之和.例如,设 X=15,Y=20,K=2,B=2,则有且仅有下列三个数满足题意 输入格 ...

  7. C# 实现一个基于值相等性比较的字典

    C# 实现一个基于值相等性比较的字典 Intro 今天在项目里遇到一个需求,大概是这样的我要比较两个 JSON 字符串是不是相等,JSON 字符串其实是一个 Dictionary<string, ...

  8. 制作MySQL的Windows服务+创建用户及授权

         在上一篇随笔中详述了MySQL的Windows 64位版本的安装,以及初始化操作.启动服务端.客户端连接.一些基本的文件操作等.然而在进行这些操作的时候,需要我们去输入一长串的路径和命令才能 ...

  9. DolphinScheduler 源码分析之 DAG类

    1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license ...

  10. PostgreSQL 实现定时任务的 4 种方法

    数据库定时任务可以用于实现定期的备份.统计信息采集.数据汇总.数据清理与优化等.PostgreSQL 没有提供类似 Oracle.MySQL 以及 Microsoft SQL Sever 的内置任务调 ...