npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-automatic-routes

v1.1.0

Published

fastest way to map directories to URLs in express

Downloads

776

Readme

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

npm install --save express-automatic-routes

:blue_book: Usage

Autoload routes

import express from 'express'
import autoroutes from 'express-automatic-routes'

const app = express()

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

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()
  }
})

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()
  }
}

Accepts params in autoroutes

: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}`)
  }
})

:arrow_forward: 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 */  },
})

: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

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]
  /* ... */
})

: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

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!')
  }
})

:arrow_forward: 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

: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):

This project follows the all-contributors specification.

Contributions of any kind welcome!