
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
file structure based router for servers
$ npm i fsbr
import router from 'fsbr';
import { createServer } from 'http';
const { use, register, route } = router();
// will be invoked on every request, before the route
use((req, res, next) => {
// do middleware things
return next();
});
register('./routes');
// will be invoked if no route was found
use((_req, res) => {
res.statusCode = 404;
res.end('Not Found');
});
use((req, res, next, error) => {
// handle error
return next(error);
});
const server = createServer(route);
server.listen(8080);
import router from 'fsbr';
import express from 'express';
const app = express();
const { use, register, route } = router();
// will be invoked on every request, before the route
use((req, res, next) => {
// do middleware things
return next();
});
use((req, res, next, error) => {
// handle error
return next(error);
});
register('./routes');
// will be invoked if no route was found
use((_req, res) => {
res.statusCode = 404;
res.end('Not Found');
});
app.use(route);
app.listen(3000);
fsbr(config: Config)
Config
default | description | |
---|---|---|
ext | .js | extension of middleware and handler files |
entry | index | name of middleware files e.g. middleware |
Creates a new fsbr
instance.
import router from 'fsbr';
const {on, use, chain, register, route} = router();
router.on(method, path, handler)
Registers a route. A Method can be any known HTTP method/verb
or a wildcard *
.
Paths can contain a variable denoted via a colon. In this case, handlers receive a third optional argument with the resolved variable. Paths can also have a wildcard. fsbr
will match every request after that.
const {on} = router();
// plain route
on('POST', '/post', (req, res) => {
//
});
// route with id parameter
on('GET', '/user/:id', (req, res, params) => {
const {id} = params;
});
// route with wildcard method
// any request with any HTTP method/verb on this route executes the handler
on('*', '/foo', (req, res) => {
//
});
// route with wildcard in pathname
// any request with '/proxy/...' executes the handler
on('GET', '/proxy/*', (req, res) => {
//
});
router.has(method, path)
Returns true, if the route exists.
const {has} = router();
has('POST', '/post');
router.use(middleware)
Registers a middleware function to the router. Middlewares with 4 parameters are considered as error handlers. Note the order. The error parameter here should be on the fourth place, unlike in other frameworks like express.
const {use} = router();
// normal middleware
use('POST', '/post', (req , res, next) => {
// do middleware things
return next();
});
// middleware for errorhandling
// notice the fourth and last argument "error"
use('get', '/photos', (req, res, next, error) => {
// handle error
console.error(error);
return next();
});
router.chain(...middlewares)
Transforms an array of middleware functions into a single middleware function.
const {chain, on} = router();
const middlewares = [
(req, res, next) => {
res.data = [];
return next();
},
(req, res, next) => {
res.data.push('foobar');
return next();
},
];
on('GET', '/custom', chain(...middlewares, (req, res) => {
console.log(res.data); // ['foobar']
}));
router.route(req, res)
Handle the incoming requests.
const {route} = router();
const server = createServer((req, res) => {
route(req, res);
});
server.listen(8080);
router.register(base, cb)
Recursively register all routes within the base
folder and call the optional callback when finished.
Each directory represents a part of the URL pathname.
Dynamic routes can be created by enclosing the directory name in square brackets e.g. [id]
.
Handlers are named by HTTP methods/verbs
and export a default listening function.
Middlewares can be placed alongside the handlers as index
files. These files can export a single middleware function or an array of functions.
const {register} = router();
register(__dirname + '/routes', () => {
console.log('done');
});
Registering the following folder/file structure with default configuration:
routes
|
├───api
| index.js
| get.js
|
├───photo
| | index.js
│ │ get.js
│ │ post.js
│ │ delete.js
│ │
│ ├───[id]
│ │ get.js
│ │
│ └───vacation
| index.js
│ get.js
│ post.js
│
└───user
post.js
Would bind the following routes:
GET:
example.com/apiGET:
example.com/photoPOST:
example.com/photoDELETE:
example.com/photoGET:
example.com/photo/:idGET:
example.com/photo/vacationPOST:
example.com/photo/vacationPOST:
example.com/userA GET call to /photo/vacation
would execute the following scripts in order:
photo/index.js
photo/vacation/index.js
photo/vacation/get.js
MIT License, see LICENSE
FAQs
file structure based router for servers
The npm package fsbr receives a total of 0 weekly downloads. As such, fsbr popularity was classified as not popular.
We found that fsbr demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.