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-clean-routes

v0.1.0

Published

express-clean-route is a library that helps to write clean routes for express applications.

Downloads

210

Readme

express-clean-routes

Build Status Coverage Status

Easy to use library that helps to write clean routes for express applications.

Install

$ npm i -s express-clean-routes

Supported HTTP Methods

get post delete put

Clean route definition

const path = [
    {
        'path' : '/users/info',
        'method' : 'get',
        'middlewares' : [middleware.checkAuth],
        'handlers' : controller.getUserInfo
    }
]

'path' can take Dynamic Routes like:

 const path = [
     {
         'path' : '/users/:id',
         'method' : 'get',
         'middlewares' : [middleware.checkAuth],
         'handlers' : controller.getUserInfo
     }
 ]

'middlewares' can take multiple middleware functions:

 const path = [
     {
         'path' : '/users/:id',
         'method' : 'get',
         'middlewares' : [middleware.checkAuth, middleware.validateData],
         'handlers' : controller.getUserInfo
     }
 ]

Usage

express-clean-routes can be used with any project structure, here lets consider the following structure

.
+-- server
|   |
|   |
|   +-- controllers
|   |   +-- index.js
|   |   +-- users.js
|   |
|   +-- middlewares
|   |   |   +-- index.js
|   |
|   +-- public
|   |
|   +-- routes
|   |   +-- index.js
|   |   +-- users.js
|   |   +-- healthcheck.js
|   |
|   app.js
|
package.json

Setting up routes

The users.js file in routes directory:


const middleware = require('../middlewares')
const controller = require('../controllers/users')

const paths = [
    {
        'path' : '/users/info',
        'method' : 'get',
        'middlewares' : [middleware.checkAuth],
        'handlers' : controller.getUserInfo
    },
    {
        'path' : '/users/info',
        'method' : 'post',
        'middlewares' : [middleware.checkAuth, middleware.checkData],
        'handlers' : controller.processUserInfo
    }
]


module.exports = paths;

The healthcheck.js file in routes directory:


const middleware = require('../middlewares')
const controller = require('../controllers')

const paths = [
    {
        'path' : '/health/status',
        'method' : 'get',
        'middlewares' : [],
        'handlers' : controller.processStatus
    },
    {
        'path' : '/health/version',
        'method' : 'get',
        'middlewares' : [],
        'handlers' : controller.processVersion
    }
]


module.exports = paths;

The index.js file in routes directory:


var paths = [];

paths.push(require('./healthcheck'));
paths.push(require('./users'));

module.exports = paths;

Setting up the middlewares

The index.js file in middlewares directory:


const Middleware = {};

Middleware.checkAuth = (req, res, next)=>{
    if(req.query.fail === 'true'){
        return res.status(401).send({message:"Auth Failed"});
    }
    next();
};


Middleware.checkData = (req, res, next)=>{
    const body = req.body;
    if(!body.hasOwnProperty("first_name")){
        return res.status(400).send({message:"bad request"});
    }
    next();
};


module.exports = Middleware;

Setting up express to use express-clean-routes (Option 1)

Your app.js will look similar to this:

const express = require('express');
const http = require('http');
const cleanroutes = require('express-clean-routes');
const routes = require('./routes');

const app = express();

app.use(express.json());

app.use('/', cleanroutes(routes));

const server = http.Server(app);
const port = 3000;

server.listen(port, () => {
  console.log(`Express server running on port ${port}`);
});

Setting up express to use express-clean-routes (Option 2)

Your app.js will look similar to this:

const express = require('express');
const http = require('http');
const cleanroutes = require('express-clean-routes');
const routes = [];

const app = express();

app.use(express.json());

// include routes here
routes.push(require('./routes/healthcheck'));
routes.push(require('./routes/users'));
app.use('/', cleanroutes(routes));

const server = http.Server(app);
const port = 3000;

server.listen(port, () => {
  console.log(`Express server running on port ${port}`);
});

Setting up express to use express-clean-routes (Option 3)

Your app.js will look similar to this:

const express = require('express');
const http = require('http');
const cleanroutes = require('express-clean-routes');
const healthRouter = require('./routes/healthcheck');
const userRouter = require('./routes/users');

const app = express();

app.use(express.json());

app.use('/', cleanroutes([healthRouter, userRouter]));

const server = http.Server(app);
const port = 3000;

server.listen(port, () => {
  console.log(`Express server running on port ${port}`);
});

See Example.

Want to contribute?

  1. Fork it!
  2. Create your feature branch: git checkout -b feature-name
  3. Commit your changes: git commit -am 'Some commit message'
  4. Push to the branch: git push origin feature-name
  5. Submit a pull request

License

ISC License (ISC)

© 2019 Shaik Sakib

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.