DEV Community

Cover image for Express Automatic Routes
Giovanni Cardamone
Giovanni Cardamone

Posted on

Express Automatic Routes

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

πŸ“ GitHub Repository

πŸ“° Full Documentation

πŸš€ Install

npm install --save express-automatic-routes
Enter fullscreen mode Exit fullscreen mode

πŸ“˜ Usage

Autoload routes

const express = require('express')
const autoroutes = require('express-automatic-routes')

const app = express()

autoroutes(app, { 
    dir: './<autoroutes-directory>' // relative to your cwd
}) 
Enter fullscreen mode Exit fullscreen mode

Create file in autoroutes directory

//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()
  }
})
Enter fullscreen mode Exit fullscreen mode

Using typescript support for module

//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()
  }
}
Enter fullscreen mode Exit fullscreen mode

Accepts params in autoroutes

ℹ️ 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}`)
  }
})
Enter fullscreen mode Exit fullscreen mode

▢️ Module definition

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 */  },
})
Enter fullscreen mode Exit fullscreen mode

▢️ 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:

ℹ️ simple middleware

export default (expressApp) => ({
  middleware: (req, res, next) => next()
  /* ... */
})
Enter fullscreen mode Exit fullscreen mode

ℹ️ array of middleware

const m1 = (req, res, next) => next()
const m2 = (req, res, next) => next()

export default (expressApp) => ({
  middleware: [m1, m2]
  /* ... */
})
Enter fullscreen mode Exit fullscreen mode

▢️ 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:

ℹ️ simple route method

export default (expressApp) => ({
  get: (req, res) => res.send('Hello There!')
})
Enter fullscreen mode Exit fullscreen mode

ℹ️ route method with middleware(s)

export default (expressApp) => ({
  get: {
    middleware: (req, res, next) => next()
    handler: (req, res) => res.send('Hello There!')
  }
})
Enter fullscreen mode Exit fullscreen mode

▢️ Skipping files

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
Enter fullscreen mode Exit fullscreen mode

⚠️ 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

πŸ“„ License

Licensed under MIT

Top comments (0)