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 发布文章使用:只允许注册用户才可以访问!
koa url path & koa-router的更多相关文章
- Django web框架-----url path name详解
说明:mytestsite是django框架下的项目,quicktool是mytestsite项目中的应用 quicktool/view.py文件修改视图函数index(),渲染一个home.html ...
- Koa框架教程,Koa框架开发指南,Koa框架中文使用手册,Koa框架中文文档
我的博客:CODE大全:www.codedq.net:业余草:www.xttblog.com:爱分享:www.ndislwf.com或ifxvn.com. Koa -- 基于 Node.js 平台的下 ...
- Redis 存储图片 [base64/url/path]vs[object]
一.base64图片编解码 基本流程:从网络获取下载一张图片.然后base64编码,再base64解码,存到本地E盘根文件夹下. import java.awt.image.BufferedImage ...
- this.getClass().getResource("") url path file 区别
首先注意 "/word/appointDismiss.docx" 前面一定要加 /,有一次我就是忘记加/ 查了半天错, 不能写成 "word/appointDismiss ...
- Nodejs file path to url path
import * as path from 'path'; import * as url from 'url'; const savePath = path.join('public', 'imag ...
- 跟我一起了解koa之在koa中使用redis
第一步安装中间件 cnpm i koa-generic-session koa-redis 第二步引入中间件 在中间件中写入session 浏览器中会存储数据 第三步关于Redis来读取和存储数据 读 ...
- 跟我一起学koa之在koa中使用mongoose(四)
第一步安装mongoose,创建数据库文件夹 第二步引入mongoose,连接数据库 第三步运行项目 这个报错 只需要将es6写法变成es5写法即可 我们连接数据库,并且以post请求的方式插入数据 ...
- gin中的重定向
package main import ( "github.com/gin-gonic/gin" ) func main() { // HTTP重定向很容易,内部.外部重定向均支持 ...
- KoaHub平台基于Node.js开发的Koa router路由插件代码信息详情
koa-router Router middleware for koa. Provides RESTful resource routing. koa-router Router mid ...
随机推荐
- MySQL设计之Schema与数据类型优化
一.数据类型优化 1.更小通常更好 应该尽量使用可以正确存储数据的最小数据类型,更小的数据类型通常更快,因为它们占用更少的磁盘.内存和CPU缓存,并且处理时需要的CPU周期更少,但是要确保没有低估需要 ...
- MySQL新特性MTS
一.MTS:多线程复制 MTS简介 在MySQL 5.6版本之前,Slave服务器上有两个线程I/O线程和SQL Thread线程.I/O线程负责接收二进制日志(Binary Log,更准确的说是二进 ...
- axios用法
1 axios.get('https://api.apiopen.top/getJoke?type=all', { 2 params: {//用于传参 3 type: 'all' 4 } 5 }).t ...
- # from tall import b from tall import * print(b) __all__ 模块 引用管理
├── __init__.py├── tall2.py└── tall.pytall.pya = 23b = 34class I: def __init__(self): print(444)clas ...
- Python Data Structure and Algorithms Tutorial
Python - Algorithm Design - Tutorialspoint https://www.tutorialspoint.com/python_data_structure/pyth ...
- Python爬虫学习笔记(三)
Cookies: 以抓取https://www.yaozh.com/为例 Test1(不使用cookies): 代码: import urllib.request # 1.添加URL url = &q ...
- Java 8新特性(Lambda,Stream API)
由于最近总监要求学习Java 8的一些知识,就去网上找了 一套教程来学习学习,将学习结果做一个小的总结记录,方便以后使用: 1.Java 8的优点 2.Lambda表达式优点 2.1Lambda实例 ...
- CCF CSP 202009-1 称检查点查询
202009-1 称检查点查询 题目背景 2020年6月8日,国务院联防联控机制发布<关于加快推进新冠病毒核酸检测的实施意见>,提出对"密切接触者"等八类重点人群&qu ...
- Effective Java读书笔记--类和接口
1.使类和成员的可访问性最小化不指定访问级别,就是包私有.protected = 包私有 + 子类一般private不会被访问到,如果实现了Serializable,可能会泄露.反射.final集合或 ...
- 看KubeEdge携手K8S,如何管理中国高速公路上的10万边缘节点
摘要:为保证高速公路上门架系统的落地项目的成功落地,选择K8s和KubeEdge来进行整体的应用和边缘节点管理. 一.项目背景 本项目是在高速公路ETC联网和推动取消省界收费站的大前提下,门架系统的落 ...