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 ...
随机推荐
- Fastjson1.2.24反序列化漏洞复现
Fastjson1.2.24 目录 1. 环境简介 1.1 物理环境 1.2 网络环境 1.3 工具 1.4 流程 2. Docker+vulhub+fastjson1.2.24 2.1 Docker ...
- 浅谈linux IO csy 360技术 2021-01-18
浅谈linux IO csy 360技术 2021-01-18
- Manachar’s Algorithm
Manachar's Algorithm Longest palindromic substring - Wikipedia https://en.wikipedia.org/wiki/Longes ...
- https://www.cs.cmu.edu/~dga/papers/cuckoo-conext2014.pdf 检验hash冲突
https://github.com/google/cityhash We like to test hash functions with SMHasher, among other things. ...
- error out of table range
.
- .params和query的区别
用法:query要用path来引入,params要用name来引入,接收参数都是类似的,分别是this.$route.query.name和this.$route.params.name.url地址显 ...
- python基础三---- time模块,函数的定义和调用
此处重点说明一下: 注意: 1.用例之间不要存在依赖关系,每个用例都可以单独运行 2.用例不要互相调用,需要调用的公共方法可以写成方法去调用 1.等待 (在脚本运行的时候,有些线程之间需要间隔时间,可 ...
- java 石头剪子布游戏
源代码 StoneGame.java 1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.I ...
- 使用FOR XML PATH实现多行数据合并成一列
有时为避免循环操作数据库.列表展示等一些原因需要将数据及关联数据批量加载进行集中处理,一种解决办法可以使用FOR XML PATH将多行数据合并成一列,达到字段拼接的效果.例如有两个表, 部门表T_D ...
- Pytest(1)安装与入门
pytest介绍 pytest是python的一种单元测试框架,与python自带的unittest测试框架类似,但是比unittest框架使用起来更简洁,效率更高.根据pytest的官方网站介绍,它 ...