express-simple-routes

Simple Routes makes it easy to import all your routes in any directory with optional authentication using Express.js

Usage no npm install needed!

<script type="module">
  import expressSimpleRoutes from 'https://cdn.skypack.dev/express-simple-routes';
</script>

README

Welcome to express-simple-routes 👋

Version Documentation Maintenance

Simple Routes makes it easy to import all your routes in any directory with optional authentication using Express.js

This package has the following features:

  • Get and use all routes in any directory
  • Easily make routes private or public
  • Protect private routes
  • Provide basic authentication via authorization header
  • - new - Provide user context for your routes (needs authentication enabled)

🏠 Homepage

Requirements

express 4.x or higher

Install

npm install express-simple-routes --save

Usage

const express = require('express')
const simple = require('express-simple-routes')
const authMiddleware = require('/custom/middleware') // optional

const app = simple(express)

app
.authenticate(authMiddleware) // optional
.routes({
    paths: ['src/routes', 'graphql/routes'],
    ignore: ['index.js']
})
.listen(4000, () => {
    console.log('🚀 Server` ready');
});

src/routes/example.js

module.exports = [
    {
        method: 'POST',
        url: '/example',
        auth: false,
        handler: (req, res, context, next) => {
            
            // Non-authenticated route
            
            res.json({
                id: 1,
                first: 'Ollie',
                last: 'Cee'
            })
        }
    },
    {
        method: 'POST',
        url: '/example/auth',
        auth: true,
        handler: (req, res, { user }, next) => {

            // User context is passed into every route if its 
            // protected. To protect a route you must have a 
            // truthy value in auth

            res.json(user)
        }
    }
]

Methods

authenticate([authMiddleware]), optional

This is whats going to allow you to return user context to your routes. You need to create authentication middleware that accepts a Bearer token in the authorization property from the header.

This is optional and you can do this with just Express using app.use() if you want more customizations.

Property Description Input Output
authMiddleware This controls whether or not a user is allowed to view content and provides user context into your [handler] token {User Object}
Examples

Middleware (authMiddleware.js)

module.exports = authMiddleware(token) {
    if (!isValidToken(token)) {
        // handle what your app does with an invalid token
    }
    return User.getByToken(token)
};

Entry point (index.js)

const authMiddleware = require('authMiddleware')

app.authenticate(authMiddleware)

routes([options]), required

This method call should happen before calling the listen method. The routes method checks all routes in every module in every directory to see if it is a valid to be used for execution.

app.routes({
    routes: [path.resolve(__dirname, 'routes')],
    ignore: ['index.js']
})
Property Description Type Default
routes Accepts a list of directories of routes to use with Express Array[String] []
ignore Controls which files get ignored in your route directories Array[String] []

listen([port], [expressCallback]), required

This method call should be the last one. It is essentially the Express instance launching the application with our routes.

app.routes(options)
   .listen(1234, () => {
      console.log('You:          Wow, amazing! Is it just this easy?')
      console.log('SimpleRoutes: Yes, now go build an amazing app!')
   })

Author

👤 olliecee hello@olliecee.com (https://olliecee.com)

🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page. You can also take a look at the contributing guide.

Show your support

Give a ⭐️ if this project helped you!