Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
apiserver-router
Advanced tools
Readme
A fast API router with integrated caching system bundled with ApiServer
⚡ npm install apiserver-router
var Router = require('apiserver-router')
// here just for example purposes,
// you don't need to call update directly, ApiServer will do it for you
router.update({
'1': {
'fooModule': {
foo: {
get: function (request, response) {
// GET http://localhost/foo/1/true => request.querystring === { id: '1', verbose: 'true' }
// GET http://localhost/foo_verbose/1 => request.querystring === { id: '1', verbose: true }
},
post: function (request, response) {
// GET http://localhost/foo/1/true => request.querystring === { id: '1', verbose: 'true' }
// GET http://localhost/foo_verbose/1 => request.querystring === { id: '1', verbose: true }
},
},
bar: function (request, response) {
// * http://localhost/1/foo_module/bar
// * http://localhost/bar
}
}
}
})
// custom routing
router.addRoutes([
['/foo', '1/fooModule#foo'],
['/foo/:id/:verbose', '1/fooModule#foo'],
['/foo_verbose/:id', '1/fooModule#foo', { 'verbose': true }],
['/bar', '1/fooModule#bar', {}, true] // will keep default routing too
])
// you can also keep your routes on a separate .js(on) file
router.addRoutes(require('./config/routes'))
For full and detailed examples look at the examples folder
new Router([options])
String
: defaults to "/:version/:module/:method") the pattern for the default routevar router = new Router({
// will disable the API versioning
defaultRoute: '/:module/:method'
})
// will shift default routes to /api
router.defaultRoute = '/api/:version/:module/:method'
Adds a custom route to the router.
The route can be both a String
or a XRegExp
. If you pass a String
, it will be compiled as XRegExp and all matches of :paramName
will create a new parameter in the request.querystring
object.
The router uses XRegExp because of its named group feature and the full RegExp support. You can play around by yourself reading the official documentation.
A custom route will disable by default the standard route associated/cached with the endpoint. You can keep the standard one passing true
as keepDefault
parameter.
defaultParameters
act as fill for missing parameters, but explicit querystring will overwrite both default
and routes parameter
.
Router.prototype.addRoute(route, methodDescription, [defaultParameters], [keepDefault])
String|XRegExp
) the custom route to match, if string will be compiled to XRegExpString
) a string in the following form "apiVersion/moduleName#methodName"Object
) an hash table containing default querystring paramentersBoolean
: defaults to false) if true the default route will not be disabledrouter.addRoute('/users/:id', '1/usersModule#getUser')
router.addRoute('/users/verbose/:id', '1/usersModule#getUser', { verbose: true })
router.addRoute('/users/:limit/:page', '1/usersModule#list', { limit: 20, page: 1 })
router.addRoute(XRegExp('/users/status/(?<message>[\\w]+)'), '1/usersModule#saveStatus', null, true)
yelds
/users/345 => { id: '345' }
/users/verbose/345 => { id: '345', verbose: true }
/users/verbose/345?id=201&verbose=false => { id: '201', verbose: 'false' }
/users/verbose/345?id=201&verbose= => { id: '201', verbose: undefined }
/users/status/coding%20likeaboss?id=34 => { id: '34', message: 'coding likeaboss' }
/users/10/2 => { limit: '10', page: '2' }
Call addRoute
n times applying the arguments array
Router.prototype.addRoutes(routes)
Array
) an Array
of Array
with addRoute
parametersrouter.addRoutes([
['/users/:id', '1/usersModule#getUser'],
['/users/verbose/:id', '1/usersModule#getUser', { verbose: true }],
['/users/:limit/:page', '1/usersModule#list', { limit: 20, page: 1 }],
[XRegExp('/users/status/(?<message>[\\w]+)'), '1/usersModule#saveStatus', null, true]
])
This method returns a list of functions that will be executed with fnchain. The list will contain all the middleware active for the API endpoint reached by pathname
and as last ring of the chain the API method to execute.
Router.prototype.get(request)
ServerRequest
) the request object already extended by the ApiServervar chanin = router.get(request)
Builds and caches the routes. You must call it every time a middleware or a module changes.
Router.prototype.update(modules[, middlewareList])
Object
) an hashtable of API modulesArray
) a list of middlewareThis repository follows (more or less) the Felix's Node.js Style Guide, your contribution must be consistent with this style.
The test suite is written on top of visionmedia/mocha and it took hours of hard work. Please use the tests to check if your contribution breaks some part of the library and add new tests for each new feature.
⚡ npm test
and for your test coverage
⚡ make test-cov
This software is released under the MIT license cited below.
Copyright (c) 2010 Kilian Ciuffolo, me@nailik.org. All Rights Reserved.
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.
FAQs
A fast API router with integrated caching system
The npm package apiserver-router receives a total of 2 weekly downloads. As such, apiserver-router popularity was classified as not popular.
We found that apiserver-router 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.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.