
Product
Introducing Pull Request Stories to Help Security Teams Track Supply Chain Risks
Socket’s new Pull Request Stories give security teams clear visibility into dependency risks and outcomes across scanned pull requests.
jk-express-automatic-routes
Advanced tools
:star: Thanks to everyone who has starred the project, it means a lot!
Plugin to handle routes in express automatically based on directory structure.
npm install --save express-automatic-routes
import express from 'express'
import autoroutes from 'express-automatic-routes'
const app = express()
autoroutes(app, {
dir: './<autoroutes-directory>' // relative to your cwd
})
//file: `<autoroutes-directory>/some/route.js`
//url: `http://your-host/some/route`
export default (expressApp) => ({
get: (request, response) => {
response.status(200).send('Hello, Route').end()
}
})
//file: `<autoroutes-directory>/some/route.ts`
//url: `http://your-host/some/route`
import { Application, Request, Response } from 'express'
import { Resource } from 'express-automatic-routes'
export default (express: Application) => <Resource> {
get: (request: Request, response: Response) => {
response.status(200).send('Hello, Route!').end()
}
}
:information_source: file/directory name must follow syntax
:paramName
or{paramName}
//file: `<autoroutes-directory>/users/{userId}/photos.js`
//mapped to: `<your host>/users/:userId/photos`
export default (expressApp) => ({
get: (request, response) => {
response.end(`photos of user ${request.params.userId}`)
}
})
each file must export a function that accept express as parameter, and return an object with the following properties:
export default (expressApp) => ({
middleware: [ /* your middlewares */ ]
delete: { /* your handler logic */},
get: { /* your handler logic */ },
head: { /* your handler logic */ },
patch: { /* your handler logic */ },
post: { /* your handler logic */ },
put: { /* your handler logic */ },
options: { /* your handler logic */ },
})
the middleware
parameter can be one of the following:
undefined
(just omit it)Middleware function
(a function complain to express middleware definition)An Array of Middleware functions
example:
:information_source: simple middleware
export default (expressApp) => ({
middleware: (req, res, next) => next()
/* ... */
})
:information_source: array of middleware
const m1 = (req, res, next) => next()
const m2 = (req, res, next) => next()
export default (expressApp) => ({
middleware: [m1, m2]
/* ... */
})
A route can be a function (likes middleware but without next
parameter) or an object who has the following properties:
examples:
:information_source: simple route method
export default (expressApp) => ({
get: (req, res) => res.send('Hello There!')
})
:information_source: route method with middleware(s)
export default (expressApp) => ({
get: {
middleware: (req, res, next) => next()
handler: (req, res) => res.send('Hello There!')
}
})
to skip file in routes directory, prepend the .
or _
charater to filename
examples:
routes
├── .ignored-directory
├── _ignored-directory
├── .ignored-js-file.js
├── _ignored-js-file.js
├── .ignored-ts-file.ts
├── _ignored-ts-file.ts
├── ignored-js-test.test.js
└── ignored-ts-test.test.ts
:warning: also any
*.test.js
and*.test.ts
are skipped!
this is useful if you want to have a lib file containts functions that don't have to be a route, so just create the file with _
prepending character
Licensed under MIT
FAQs
fastest way to map directories to URLs in express
We found that jk-express-automatic-routes 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.
Product
Socket’s new Pull Request Stories give security teams clear visibility into dependency risks and outcomes across scanned pull requests.
Research
/Security News
npm author Qix’s account was compromised, with malicious versions of popular packages like chalk-template, color-convert, and strip-ansi published.
Research
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.