项目作者: GiovanniCardamone

项目描述 :
fastest way to map directories to URLs in express
高级语言: JavaScript
项目地址: git://github.com/GiovanniCardamone/express-automatic-routes.git
创建时间: 2020-08-15T18:11:57Z
项目社区:https://github.com/GiovanniCardamone/express-automatic-routes

开源协议:MIT License

下载


express-automatic-routes



Logo

JavaScript TypeScript

NPM version
NPM downloads
Known Vulnerabilities
GitHub license

CI
Coverage Status

:star: Thanks to everyone who has starred the project, it means a lot!

Plugin to handle routes in express automatically based on directory structure.

:newspaper: Full Documentation

express-automatic-routes

:rocket: Install

  1. npm install --save express-automatic-routes

:blue_book: Usage

Autoload routes

  1. import express from 'express'
  2. import autoroutes from 'express-automatic-routes'
  3. const app = express()
  4. autoroutes(app, {
  5. dir: './<autoroutes-directory>' // relative to your cwd
  6. })

Create file in autoroutes directory

  1. //file: `<autoroutes-directory>/some/route.js`
  2. //url: `http://your-host/some/route`
  3. export default (expressApp) => ({
  4. get: (request, response) => {
  5. response.status(200).send('Hello, Route').end()
  6. }
  7. })

Using typescript support for module

  1. //file: `<autoroutes-directory>/some/route.ts`
  2. //url: `http://your-host/some/route`
  3. import { Application, Request, Response } from 'express'
  4. import { Resource } from 'express-automatic-routes'
  5. export default (express: Application) => <Resource> {
  6. get: (request: Request, response: Response) => {
  7. response.status(200).send('Hello, Route!').end()
  8. }
  9. }

Accepts params in autoroutes

:information_source: file/directory name must follow syntax :paramName or {paramName}

  1. //file: `<autoroutes-directory>/users/{userId}/photos.js`
  2. //mapped to: `<your host>/users/:userId/photos`
  3. export default (expressApp) => ({
  4. get: (request, response) => {
  5. response.end(`photos of user ${request.params.userId}`)
  6. }
  7. })

:arrow_forward: Module definition

each file must export a function that accept express as parameter, and return an object with the following properties:

  1. export default (expressApp) => ({
  2. middleware: [ /* your middlewares */ ]
  3. delete: { /* your handler logic */},
  4. get: { /* your handler logic */ },
  5. head: { /* your handler logic */ },
  6. patch: { /* your handler logic */ },
  7. post: { /* your handler logic */ },
  8. put: { /* your handler logic */ },
  9. options: { /* your handler logic */ },
  10. })

:arrow_forward: Middleware module definition

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

  1. export default (expressApp) => ({
  2. middleware: (req, res, next) => next()
  3. /* ... */
  4. })

:information_source: array of middleware

  1. const m1 = (req, res, next) => next()
  2. const m2 = (req, res, next) => next()
  3. export default (expressApp) => ({
  4. middleware: [m1, m2]
  5. /* ... */
  6. })

:arrow_forward: Route definition

A route can be a function (likes middleware but without next parameter) or an object who has the following properties:

  • middleware // same as module middleware
  • handler // the handler of the function

examples:

:information_source: simple route method

  1. export default (expressApp) => ({
  2. get: (req, res) => res.send('Hello There!')
  3. })

:information_source: route method with middleware(s)

  1. export default (expressApp) => ({
  2. get: {
  3. middleware: (req, res, next) => next()
  4. handler: (req, res) => res.send('Hello There!')
  5. }
  6. })

:arrow_forward: Skipping files

to skip file in routes directory, prepend the . or _ charater to filename

examples:

  1. routes
  2. ├── .ignored-directory
  3. ├── _ignored-directory
  4. ├── .ignored-js-file.js
  5. ├── _ignored-js-file.js
  6. ├── .ignored-ts-file.ts
  7. ├── _ignored-ts-file.ts
  8. ├── ignored-js-test.test.js
  9. └── 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

:page_facing_up: License

Licensed under MIT

:sparkles: Contributors

Thanks goes to these wonderful people (emoji key):






Giovanni Cardamone

💻 📖 💡 🚧

This project follows the all-contributors specification.

Contributions of any kind welcome!