Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
autoroute-express-promise
Advanced tools
Auto-route Express Promise
routing made simple with a single place to update.
Install with npm
:
npm install --save autoroute-express-promise
import {routes} from 'autoroute-express-promise'
import isUserAuthenticated = require('../controllers/auth/auth')
import express = require('express')
import withMessageAs = require('../utilities/response-structure')
var router = express.Router()
var authenticatedRoute = route => router.route(route).all(isUserAuthenticated)
routes({
baseRoute: authenticatedRoute,
response: (response, result) => response.send(withMessageAs(result)),
message: (o) => {console.log(o.routeName, o.methodName)}
}, ['./controllers/**/index.js'])
module.exports = router
File Structure:
Note, that this is how I do my file structure. Any file structure is OK.
├── controllers/
├── api1/
│ └── index.js
├── api2/
│ └── index.js
└── api3/
└── index.js
Code Structure:
Note that you can do an array of routes also.
import {method} from 'autoroute-express-promise'
import {getPet, petDied} = require('../../pets')
const PETS = '/pets',
ID = 'petId',
PET = PETS + '/:' + ID
var routes: AutoRouteExpressPromise.RouteDefinition = {
route: PET,
methods: [
[ method.get, (req: Request) => getPet(req.params[ID]) ],
[ method.delete, (req: Request) => petDied(req.params[ID]) ] ]
}
export = routes
Example with two routes:
import {method} from 'autoroute-express-promise'
import {getPet, newPet, petDied} = require('../../pets')
const PETS = '/pets',
ID = 'petId',
PET = PETS + '/:' + ID
var routes: AutoRouteExpressPromise.RouteDefinition[] = [
{ route: PETS,
methods: [
[ method.post, (req: Request) => getPet(req.body) ] ]
},
{ route: PET,
methods: [
[ method.get, (req: Request) => getPet(req.params[ID]) ],
[ method.delete, (req: Request) => petDied(req.params[ID]) ] ]
}
]
export = routes
Cleaner nested route syntax (version 0.2+):
var routes = {
'/pets': {
methods: [
[ method.post, (req: Request) => getPet(req.body) ] ]
':id': {
methods: [
[ method.get, (req: Request) => getPet(req.params[ID]) ],
[ method.delete, (req: Request) => petDied(req.params[ID]) ] ]
}
}
}
import {routes, method} from 'autoroute-express-promise'
routes(options, glob[])
:where options
:
{
baseRoute: (routeName: string) => any
message?: (options: {routeName: string; methodName: string}) => void
response: (client: Express.Response, result: any) => any
}
baseRoute: (Required) Uses route name (e.g., /api/myroute/:id
) and return
an express.js Router
. E.g.,
var router = express.Router
var authenticatedRoute = route => router.route(route).all(isUserAuthenticated)
message: (Optional) A function which passes the route name and method name (post, get, etc). Used for writing logs when route is called by server, e.g.,
o => {console.log(o.routeName, o.methodName)}
where: o
is {routeName: '/api/myroute/:id', methodName: 'get'}
.
response: (Required) Used to wrap the result in some wrapper and send method, e.g.,
(client, result) => client.send({result: result})
where glob
:
List of globs as specified in
require-glob
. E.g.,
['./controllers/**/index.js']
.
method
:This is an enumeration of all the express.js
methods:
get, post, put, head, delete, options, trace, copy, lock, mkcol, move, purge, propfind, proppatch, unlock, report, mkactivity, checkout, merge, "m-search", notify, subscribe, unsubscribe, patch, search, connect
E.g.,
method.get // => 0
method[0] // => "get"
FAQs
autoroute for use with promises
We found that autoroute-express-promise 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
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.