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

controllers

v0.0.2

Published

A simple mvc framework and route extender for Express

Downloads

1,309

Readme

Controllers

A simple mvc framework and route extender for Express.

Installation

$ npm install controllers

Usage

After setting all your middleware in Express, call the controllers method to initialise.

express = require 'express'
controllers = require 'controllers'

app = express.createServer()
app.use(express.static(__dirname + '/public'));

# Make sure all your app.use statements have been called
controllers app, options

Your folder system should now look like the following:

site
 |-> controllers
 |   |-> home.js (or coffee, if you have overwritten the require calls)
 |   |-> blog.js
 |-> views
		 | -> home
		 |    |-> index.jade (these are your views, use whatever renderer you want)
		 |    |-> welcome.jade
		 | -> blog
		 |    | -> index.jade
		 | -> shared
		 			| -> layout.jade

Controllers are called depending on your routing, and the render call is overwritten to access the folder with the same name as the controller, falling back to the shared folder if needed.

Routing

When routing a controller and action must be defined, controllers extends the routing in Express to allow for default values

# app.get 'route', defaults, middleware...
app.get '/blogPage', { controller: 'blog', action: 'index' }, middleware
app.get '/:controller?/:action?/:id?', { controller: 'home', action: 'index' }, middleware

The above routing will route the following paths:

'/' -> Routes to the controller 'home' and runs the method 'index'
'/blogPage' -> Routes to the controller 'blog' and runs the method 'index'
'/home/welcome/1' -> Routes to the controller 'home' and runs the method 'welcome', with the 'id' param set to 1

What does a controller look like?

The controller actions follow the normal convention of Express, taking the request, response and next arguments:

module.exports.index = (req, res, next) ->
	res.render()
			
module.exports.welcome = (req, res, next) ->
	id = req.param.id ?? 0
	res.partial { id: id }

The render does not take an argument as the view for this action is automatically searched for in at 'views/home/index' and if that fails falls back to 'views/shared/index'.

Helpers

There are a number of useful calls available in the controllers and views.

Controllers:

req.controller  # stores current controller
req.action # stores current action
req.executeController 'controller', 'action', cb # Executes another controller and overwrites the next function with the cb

Views:

controller # stores current controller
action # stores current action
getUrl 'controller', 'action', defaultParams, queryParams # returns a url corresponding to the controller/action specified
getUrl 'action', defaultParams, queryParams # same as above but using the current controller

Options

The default options are:

# If the controller/action is not defined do we throw an exception?
strict: true  

# Overwrite the render/partial calls to use the 'controller/action' breakdown of the views
overwriteRender: true

# Log when the controllers are loaded and called
log: false

# Set the root folder for the controllers
root: app.set('controllers') || process.cwd() + '/controllers'

# Set the share folder in the views, all render/partial calls will fall back to this folder
sharedFolder: 'shared'

License

©2012 Felix Jorkowski and available under the MIT license:

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.