xiongwilee / Gracejs

A Nodejs BFF framework, build with koa2(基于koa2的标准前后端分离框架)
https://grace.wilee.me
MIT License
1.39k stars 238 forks source link

正则路由 #30

Closed darkforest42 closed 7 years ago

darkforest42 commented 7 years ago

比如说我要生成这样一个路由: http://127.0.0.1/company/development company是controller目录下的一个路由文件,而后面的development是不确定的分类,我本想在company.js下设置regular为正则匹配, 结果正则并不起作用

xiongwilee commented 7 years ago

这样试试:

// company.js
exports.index = async function(){
  // controller
}
exports.index.__regular__ = /^\/company(?:\/|$)/
darkforest42 commented 7 years ago

正则有问题 模块path-to-regexp报错了 SyntaxError: Invalid regular expression: /^\/company\/index\/\^\/company((?:?\:\ \/|\$))(?:\/(?=$))?$/: Nothing to repeat 然后我改成/^\/company(\/(\S)+)?/,访问http://127.0.0.1/company/development, Not Found

xiongwilee commented 7 years ago

我这儿没有问题:

// company.js
exports.index = async function(){
  this.body = 'test'
}
exports.index.__regular__ = /^\/company(?:\/|$)/
darkforest42 commented 7 years ago

Oh,原来是我没有更新代码 你有一次commit : update添加正则表达式路由的支持