fast-custom-router
v1.4.2
Published
Fast custom express router.
Downloads
12
Readme
import { Parser } from 'fast-custom-router'
const parser = new Parser(app, config)
parser.parseFromFile('config.yml')
parser.load()
fast-custom-router
This is a powerful custom enrober for express router.
Installation
This is a Node.js module available through the npm registry.
Before installing, download and install Node.js v14.X.X or higher.
Installation is done using the npm install command:
npm install fast-custom-router
Features
- strict configuration parser
- dynamic module imports (middlewares and controllers)
- type checking (uri params and body params)
- modular configuration files (with imports)
Quick start
First of all, you will need to use a router such as Express.
Then create one configuration file, for example:
# config/config.yaml
api:
root: /api
pre_middlewares:
- authenticate
post_middlewares:
- errorHandler
routes:
user:
path: /user/:id
params:
id: number
methods:
GET:
controller: user/user:getUser
DELETE:
controller: user/deleteUser
pre_middlewares:
- checkUserPermissions
post_middlewares:
- logRequests
Then your main script launcher:
// main.js
import { dirname, join } from 'path'
import { fileURLToPath } from 'url'
import http from 'http'
import Express from 'express'
import { Parser as APIParser } from 'fast-custom-router'
/** Configuration */
const __dirname = dirname(fileURLToPath(import.meta.url))
const config = {
config_dir: join(__dirname, 'config'),
controller_dir: join(__dirname, 'controller'),
middleware_dir: join(__dirname, 'middleware'),
}
/** Define servers */
const app = Express()
const server = http.createServer(app)
const parser = new APIParser(app, config)
parser.parseFromFile('config.yaml')
parser.load()
server.listen(8000, () => {
console.log('[SERVER][INFO] Server started on port 8000')
})
And finnaly create middlewares and controllers
// middleware/authenticate.js
export default function authenticate(req, res, next) {
// Doing stuff here
next()
}
// [...]
// controller/user.js
export function getUser({params}) {
return {
message: `User #${params.id} has been requested !`
}
}
// controller/deleteUser.js
export default function deleteUser({params}) {
return {
message: `User #${params.id} has been deleted !`
}
}
Documentation
Philosophy
The package philosophy is to provide small a smart configuration parser that load an application into a router. The package must be robust, fast (in execution time way), and fast (in writting code way).
The key words are :
- robust
- modularity
- easy-to-use
- light
It does not depends on any specific router, but we recommand to use with Express.
Tests
The library provide tests, and every commit is checked by using Github Actions : .
If you want to run the tests by yourself, please follow this commands :
git clone [email protected]:vigilock/fast-custom-router.git
cd fast-custom-router
npm install
npm test
License
At Consignity we chosen the MIT License as open-sourced license.