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

spatcher

v0.1.8

Published

Spatcher is helping the creation of routes in the express framework, routes do not need to be all written when the controllers lie in a folder and contain a range of actions

Downloads

4

Readme

Build Status

spatcher is here to help you to avoid manually writing all routes to dispatch requests in an express app.

Getting started

Your files should look something like:

myAppName/
	app/
		controllers/
			helloController.js
			indexController.js

Your helloController.js looks like:

module.exports = {
	'fooAction': function(req, res) {
		res.send("foo of hello", {'Content-Type': 'text/html'}, 200);
	},
	'indexAction': function(req, res) {
		res.send("index of hello", {'Content-Type': 'text/html'}, 200);
	},
};

Your indexController.js looks like:

module.exports = {
	'indexAction': function(req, res) {
		res.send("index of index", {'Content-Type': 'text/html'}, 200);
	},
};
npm install --save spatcher

Then in your main application file, app.js :

var app = require('express')();
var spatcher = require('spatcher');

var spatcherInstance = spatcher(app, 'myAppName/app/controllers');

// then you just process as you are used to.
var server = app.listen(3000, function() {
  console.log('Listening on port %d', server.address().port);
});

Launch your server:

node app.js

Then you can just go to:

http://localhost:3000/
http://localhost:3000/hello/
http://localhost:3000/hello/foo

Advanced usage

require('spatcher')()

The function returned by the module can be called without any argument. The returned object can be configured and then used to route requests.

var spatcherInstance = require('spatcher')();

spatcherInstance.appendControllerToName = false; 
spatcherInstance.appendActionToName  = false;
spatcherInstance.errorOnActionNameLeadingUnderscore = false;

// The path in which controllers will be looked up
var controllersModulePath = 'myApp/myControllers';
// the url prefix to use (default is nothing) in order to look for controllers
var urlPrefix = '/somePrefixInTheUrl';
spatcherInstace.route(expressAppInstance, controllersModulePath, urlPrefix);

require('spatcher')(app, controllersModuleRootPath, urlPrefix)

If you call the module with some arguments, an object is instantiate and the route() method is directly called with the provided arguments.

Multiple controller route and chaining

spatcher handles multiple routes calling.

var app = require('express')();
var spatcherInstance = require('spatcher')();

spatcherInstance.route(app, 'myapp/backoffice/controllers');
spatcherInstance.route(app, 'myapp/frontoffice/controllers');
spatcherInstance.route(app, 'someexternalstuff/controllers', 'extrautil');

Configuration

Configuration can be done before or after wiring the routes.

var app = require('express')();
var spatcherInstance = require('spatcher')(app, 'app/mycontrollers');

// This option is true by default and happens "Controller" to the name of
// the called module
spatcherInstance.appendControllerToName = false; 

// This option is true by default and happens "Action" to the name
// of the called function
spatcherInstance.appendActionToName  = false;

// This option is true by default, this blocks the call to any function prefixed
// by an underscore (the common naming convention for private function)
spatcherInstance.errorOnActionNameLeadingUnderscore = false;

// ...

If you want different configuration for different contexts, you have to call spatcher another time.

// ...

var secondSpatcherInstance = require('spatcher')(app, 'somemodule/somecontrollersrootpath');

// ...

Parameters extraction ---

If you want sexy urls, you can!

http://myserver.com/something/myTool/a/1/b/2/c/3/a/checkThat

The req (first) parameter received by your somethingController module's myToolAction function will contain a key a value ['1', 'checkThat'] and you guess that b and c will hold '2' and '3'.

Origin

The code base is mostly an extension and a rewrite of some express boilerplate code (I can't find it anymore).

License

MIT