0.0.1 • Published 10 years ago

touchngo v0.0.1

Weekly downloads
2
License
MIT
Repository
bitbucket
Last release
10 years ago

touchngo (Configure Express like a UNIX Admin)

Do you use Express? Do you like to get down and dirty in the Terminal?

Try this on for size! Go into the top-level directory of your Express app:

npm install touchngo --save
mkdir middleware
cd middleware
touch 10_static-favicon \
      30_body-parser.json \
      40_body-parser.urlencoded \
      50_cookie-parser
echo '["dev"]' > 20_morgan
echo '["./public"]' > 60_express.static

Now just replace all your stupid app.use(middleware) crap with:

require('touchngo').middleware(app, './middleware');

Note: The './middleware' argument was completely optional.

So here's what just happened. When you did:

touch 10_static-favicon

You were telling touchngo to load the Express middleware module called 'static-favicon' at priority 10. touchngo will first search for the module using relative pathing from the top of your app. If that doesn't work, it'll switch to absolute pathing.

Technically, you can load any module. Just separate out its path components with dots (.). If touchngo can't find the module, it will also attempt to resolve the middleware as an explicitly exported function of a module. For example:

touch 40_body-parser.urlencoded

Here, touchngo first tries to resolve using require('body-parser/urlencoded'). If that doesn't work (and it won't) then it will try to resolve using require('body-parser').urlencoded.

The leading digits are the priority at which you want the middleware loaded. You can use as many digits as you like, we'll parse them all the same. You can duplicate priorities, but it's your problem if things don't work out. You can also leave out trailing zeros, but that's also your problem if the output of ls -l confuses you.

You can also load middlewares into specific environments. By default, we just load them without prejudice, but here's how you'd load the 20_morgan middleware into only 'development' and 'staging':

echo '["dev"]' > 20_morgan:development,staging

Or if you're on Windows (why???) and the : and , will be a problem:

echo '["dev"]' > 20_morgan@development.staging

Note: Of couse, if you only want to load it into one environment, you don't need the delimiting at all:

echo '["dev"]' > 20_morgan:development

Current Status

I just wrote this on a plane ride. I've started by supporting middleware, but I might extend it into routing as well. In any case, I'll finish it later.

License (MIT License)

Copyright (c) 2014 Thomas S. Bradford

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.