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-route-control

v1.0.2

Published

Express Route Controller

Downloads

15

Readme

express-route-control

Express Routes Controller, using simple file and folder structure.

This module takes care of your routes and can provide you with different formatting of your route controllers. You can use a plain function or an object defining your route options.

Installation

Add this dependency iin your package.json file

	"dependencies": {
    	"express-route-control": "1.0.0"
    }

and run npm install in your console, while in your application root folder.

Usage

In your app.js file you configure the router:

var routes = require('express-route-control'),

...
..
.

routes(app, {
			 dir: __dirname + "/your_folder_location",
             rootFileName: 'some_name' // defaults to 'index'
       });

===================== TODO's add more options and a view folder to each module.

and a folder structure like this:

/your_folder_location/
	index.js
    other_route_name.js
    /parent_route_folder/
    	/child_route_folder/
        	index.js
    	index.js

This structure will create routes:

  • www.example.com
  • www.example.com/other_route_name
  • www.example.com/parent_route_folder
  • www.example.com/parent_route_folder/child_route_folder

If no rootFileName property is defined, index is considered as default. Index file takes care of the root path handling. For example if you have a route 'www.example.com', that file will handle the actions for those routes, if you have a route 'www.example.com/other_route_name', you must either have an action in your index.js file named other_route_name, or have a subdirectory named other_route_name.

With this approach you can easily maintain your code and separate your logic for different routes. In other words you can create modules.

Now to we are going to show you how to define handlers / controllers for each path.

In the /your_folder_location/index.js, create this function:

exports.index = function(req, res) {
	...your logic
};

this function takes care of the 'www.example.com path, if the same function is located in the /your_folder_location/parent_route_folder/index.js, will handle this route: www.example.com/parent_route_folder

You can also format this handler like this:

exports.index = {
	type: 'get', // default type, you can pass 'post', 'update', 'delete, 'put'
	route: function(req, res) {
		...your logic
	}
};

or like this:

exports.routes = {
	'index': {
		type: 'get',
		route: function(req, res) {
			...your logic
		}
	},
	'other_route': function() {
		...your logic
	}
};

As you can see all of these formats can create the same path handling, it can all depend on what do you need in this route.

Context / scope of the route function is the Route object, which can help you in your handling:

Route Object

req

Type: Request

Original Express Request object

res

Type: Response

Original Express Response object

renderPage

Type: Function Arguments: String page, Object page render context

Renders the passed page with given context

render

Type: Function Arguments: Object page render context

Resolves the page name so it resemebles the current location of the file and action name, with given context.

renderJSON

Type: Function Arguments: Object JSON response

Renders a JSON object as a response.

===================== TODO's add more options.

Route Options

route

Type: Function Default: Returns next()

Function that takes care of the route handling, aka the Controllah.

type

Type: String Default: get

This value is what request method do you expect. It is not case sensitive, and it can be GET, POST, PUT, DELETE

action

Type: String Default: to the name of the key/exported method

Defines the route name this controller should handle.

filter

Type: Function Default: false

Deifines a filter function that must be called before calling the route function.

===================== TODO's add parameter bindings.

Filters

You can also add a filter to each Controller file, which will be applied to all actions contained ot that controller path. Filter is defiined like this:

 exports.filters = function(req, res, next) {
	if(req.session.user) next();
	res.send(403);
};

or with an deny parameter, witch specifically tells that this route, or routes, when hit must pass the filter:

 exports.filters = {
	deny: "post, send",
	filter: function(req, res, next) {
		if(req.session.user) next();
		res.send(403);
	}
};

Each action can also contain a filter. You can define it like this:

exports.index = {
	type: 'get',
	filter: function(req, res, next) {
		...your logic
	},
	route: function(req, res) {
	}
};