克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

fc-route

Coverage Status Coverage Status

一个轻便高效的node路由

支持原生node的http,https,http2模块,支持koa框架,经过高并发测试,性能优越,精确匹配,使用方便

安装npm包

npm install fc-route

使用方法

http:

const http = require('http');
let server = http.createServer();
const _router = require('fc-route');
let router = new _router();
router.get('/',function (req, res) {
	res.end('hello world');
});

router.http(server);
server.listen(3000, function () {
	console.log('服务器3000启动成功,可以访问了。。。')
})

https:

const https = require('https');
const fs = require('fs');
const  server = https.Server({
    key: fs.readFileSync(__dirname+'/server.key'),
    cert: fs.readFileSync(__dirname+'/server.crt'),
});
const _router = require('fc-route');
let router = new _router();
router.get('/',function (req, res) {
	res.end('hello world');
});

router.http(server);
server.listen(3001, function () {
	console.log('服务器3001启动成功,可以访问了。。。')
})

http2:

const http2 = require('http2');
const fs = require('fs');
const server = http2.createSecureServer({
    key: fs.readFileSync(__dirname+'/server.key'),
    cert: fs.readFileSync(__dirname+'/server.crt'),
});
const _router = require('fc-route');
let router = new _router();
router.get('/',function (req, res) {
	res.end('hello world');
});

router.http(server);
server.listen(3002, function () {
	console.log('服务器3002启动成功,可以访问了。。。')
})

koa:

const Koa = require('koa');
const app = new Koa();
const _router = require('fc-route');
let router = new _router();
router.get('/', function (ctx, next) {
	ctx.body = 'hello world';
});

router.koa(app);
server.listen(3004, function () {
	console.log('服务器3004启动成功,可以访问了。。。')
})

更多使用demo请看这里 >>> 戳这里

路由方法

方法 说明
get router.get('/',function (req, res){})
post router.post('/',function (req, res){})
head router.head('/',function (req, res){})
options router.options('/',function (req, res){})
put router.put('/',function (req, res){})
patch router.patch('/',function (req, res){})
delecte router.delecte('/',function (req, res){})
all router.all('/',function (req, res){})

all方法匹配所有,请谨慎使用 为了安全考虑,一些特殊的http方法并没有做适配

路由匹配符

匹配符号 作用 代码
% 匹配数字 router.get('/%value',function (req, res){})
@ 匹配大小写 router.get('/@value',function (req, res){})
: 匹配大小写数字 router.get('/:value',function (req, res){})
! 匹配中文 router.get('/!value',function (req, res){})
~ 匹配以上全部 router.get('/~value',function (req, res){})

~匹配所有字符串,此项能匹配以上的所有路由,请放在第二匹配上,第一匹配请不要用,请慎重选择

匹配取参数

router.get('/%sz/@zm', function (req, res) {
	let { sz, zm } = req.params;
	res.end('数字:'+sz+',字母:'+zm);
});

全局设置

router.get(function (req, res) {
	res.write('所有的get方法,这里都会输出');
});

设置路由前缀

router.prefix('/app')

精确匹配原则

当你设置两行规则的时候,会优先匹配最精准的路由

router.get('/%sz/@zm', function (req, res) {
	let { sz, zm } = req.params;
	res.end('数字:'+sz+',字母:'+zm);
});
router.get('/6/@zm', function (req, res) {
	let { zm } = req.params;
	res.end('字母:'+zm);
});

按照一般的路由匹配,上面那个匹配肯定会覆盖下面的,实际上则不然,当你第二个参数为6的时候,只会匹配第二个 这个精确匹配功能koa的路由并不具备,无需担心效率,内部有缓存机制

更新记录

2023/10/06

0.0.5 => 现在不用担心使用原生http,处理逻辑写错了会中断了进程,另外新加了res.json来帮你快速输出json格式

router.get('/',function (req, res) {
	res.json({'code':200,'msg':'sucess'});
});

License

MIT

Copyright (c) 2022-lovefc

MIT License Copyright (c) 2022 封尘 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

通用node路由匹配器 展开 收起
JavaScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化