Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
#Routing
####介绍
Routing是基于express的自动设置路由模块
var routing = require('routing');
####关于自动路由
Routing读取controllers文件夹内的xxxController.js文件,并缓存所有action函数
例:
以下controllers是目录结构
--- controllers/ controllers文件夹(可自定义文件夹,在config.js内修改)
|---- indexController.js controller文件(必须有Controller,可自定义在config.js内修改)
| |---- indexAction action函数(必须有Action,课自定在config.js内修改)
| |---- signupAction
| |---- ....
|
|---- homeController.js
|---- userController.js
|---- ....
|
|---- topic/
|---- indexController.js
|---- ....
以下views是目录结构
--- views/ views文件夹(可自定义文件夹,在config.js内修改)
|---- index/ controller文件(必须有Controller,可自定义在config.js内修改)
| |---- index.html action函数(必须有Action,课自定在config.js内修改)
| |---- signup.html
| |---- layout.html
| |---- ....
|
|---- home/
|---- user/
|---- ....
|
|---- topic/
| |---- index/
| |---- ....
当接受请求为/index/index时,自动路由到indexController.js内的indexAction函数,默认action为index(可在config.js内修改),默认controller为indexController(可修改)
当接受请求为/index/index/id/123/type/submit时,自动路由同上,action函数内可获取参数,使用request.params['id']可以获取得值123,request.params['type']可以获取得值'submit'
自动路由没有找到对应action则抛出404错误转由错误视图控制,详见错误视图
action函数内可以使用respon.template函数调用对应views下相应视图
####关于错误视图
启用错误视图请确认设置
config.js文件内config.debug = 0;
任何时刻抛出错误都可以在错误视图中捕获并调用对应视图,默认调用500视图对象
例:throw new Error('404') 会调用已定义的404错误视图
可以用使用routing.errorCode对象自定义视图,默认只有404和500视图
####关于api
customRoute
customRoute函数接受一个函数参数,用于自定义路由,拥有最高优先级,允许多次调用
例:
routing.customRoute(function() {
routing.app.get('/index',function(req,res,next) {
.........
})
routing.app.post('/:id',function(req,res,next) {
.........
})
})
routing.customRoute(function() {
this.all('/index/user',function(req,res,next) {
.........
})
this.post('/:id',function(req,res,next) {
.........
})
})
参数函数内推荐使用this定义路由,定义方法和express定义路由相同,this.get()|this.post()|this.all()
....
listen
listen函数用于配置好后开启服务routing.listen(80);
其他变量
routing.app 存储express的server实例
routing.controllers 存储缓存的controller对象
routing.errorCode 存储存储错误视图对象
####安装
安装方法 npm install routing
routing依赖于express 已经依赖了express不需要再安装express
####其他
- Routing已经配置好生产环境(production)和开发环境(development)
- 默认存放controller文件夹为/controllers,默认使用ejs模板引擎
- 静态文件不会使用到自动路由
- 须自己修改config.js文件
FAQs
Automatic routing based on express
We found that routing demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.