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

clay-router

v1.2.3

Published

express router

Downloads

21

Readme

clay-router

This is a package developed to group Express routes. Clay-router can help you to order your project splitting your router logic in controllers files and middlewares files.

controler file is a javascript file which contains an object with methods to handle a particular route.

middleware file is a javascript file which contains an object with methods to handle routes before the route be handle by a controller method.

Install

To start with clay-routes you have to install express and clay-router package.

$ npm install express

$ npm install clay-router

How to use

  1. Create controllers and middlewares folders.

    $ mkdir controllers
    $ mkdir middlewares
  2. Create controllers and middlewares with the following schema.

    controllers/myIndexController.js

    let myIndexController = {
    	myMethod1:(req,res)=>{
    		res.send({msg:'this is the first method'})
    	},
    	myMethod2:(req,res)=>{
    		//this method use a req.test value setted in the middleware file
    		res.send({msg:req.test})
    	}
    }
    
    module.exports = myIndexController

    middlewares/myMiddleware.js

    let myMiddleware = {
    	myMethod1:(req,res, next)=>{
    		req.test = 'abcd'
    		next()
    	}
    }
    
    module.exports = myMiddleware

    You can create controllers files an middlewares files as many you want.

  3. Create an clay-router instance.

    var clay = require('clay-router')
    clayRouter = new clay({
    	controllerPath:'controllers',
    	middlewarePath:'middlewares'
    })
    
  4. Group your routes and set the controllers and middlewares.

    clayRouter.group('/test',['myMiddleware@myMethod1'],[
    	{method:'get', path:'/test1',handler:'myIndexController@myMethod1'},
    	{method:'get', path:'/test2',handler:'myIndexController@myMethod2'}
    ])
    

    Group has three arguments:

    • The principal url ('/test')

    • An array of middlewares: a string with the following structure

      'middlewarefilename@method'
    • An array of routes: routes are objects with the following structure:

      	{
      		method:'get/post', 
      		path:'subpath',
      		handler:'controllerfilename@method'
      	}
  5. Use the router property to register your routes in your Express app. Your app file will looks like this.

    var express = require('express');
    var app = express();
    var clay = require('clay-router')
    
    
    clayRouter = new clay({
    	controllerPath:'controllers',
    	middlewarePath:'middlewares'
    })
    clayRouter.group('/test',['myMiddleware@myMethod1'],[
    	{method:'get', path:'/test1',handler:'myIndexController@myMethod1'},
    	{method:'get', path:'/test2',handler:'myIndexController@myMethod2'}
    ])
    
    //you can use express methods in the router property object like that
    clayRouter.router.get('/index',(req,res)=>{
    	res.send('index')
    })
    clayRouter.router.get('/example',(req,res)=>{
    	res.send('example')
    })
    
    //register the routes
    app.use('/',clayRouter.router)
    
    app.listen(3000, function () {
    	console.log('Example app listening on port 3000!');
    })