Socket
Socket
Sign inDemoInstall

express-load

Package Overview
Dependencies
0
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    express-load

Autoload modules into an Express application instance


Version published
Weekly downloads
551
increased by12.68%
Maintainers
1
Install size
10.9 kB
Created
Weekly downloads
 

Readme

Source

Express Load

The express-load module provides the ability to load scripts into an Express instance from a specified directory. Make large express MVC applications easier to develop by allowing a logical file separation without having to include a bunch of files, see the examples folder for information.

Despite being a very simple module, it is extremely useful. It can be used to autoload models, routes, schemas, configs, controllers, object maps... etc...

express-load gives you access to the autoloaded files in the Express application instance to keep out of the global namespace. This also allows access to the scripts via the request object.

A script at controllers/user.js becomes available as app.controllers.user or req.app.controllers.user in a request.

Installation

$ npm install express-load

Usage

Multiple Directories

require('express-load')([
	'models',
	'controllers',
	'routes'
], app);

Single Directory

require('express-load')('routes', app);

The first parameter can be an array of directories or a string. The second parameter must be the Express application instance.

Autoload Configuration

The autoConfigure feature loads and configures multiple environment configurations in Express.

var load = require('express-load');

load('config', app)
  .autoConfigure(app.config);

This will now use the Express app.configure() function to load each environment configuration using app.set(). Once this is called the configuration options in the current environment are available in app.get() or app.settings. See the configuration example in the examples folder.

The app.config is passed in as a parameter which is the newly available configuration environments, this is required in instances when you may load more the just config:

load(['config', 'models', 'routes', 'controllers'], app)
  .autoConfigure(app.config);

Remember that if you name your configuration file something else, for example configuration the environments will then be available at app.configuration.

Simple Express Load Example

app.js

var express = require('express')
  , load = require('express-load');

var app = express();

load(['controllers', 'routes'], app);

app.listen(3000)

If there were the following files in the controllers folder:

user.js
post.js
comment.js

They would then be available as: app.controllers.user app.controllers.post app.controllers.comment

Or from within a request as: req.app.controllers.user req.app.controllers.post req.app.controllers.comment

The directories are read synchronously, this is only done once when the app starts allowing the directories listed to have the scripts loaded in the order specified, for example you will want to load the controllers before the routes.

More examples will be available in the examples folder.

License

(The MIT License)

Copyright (c) 2012 Jarrad Seers <jarrad@jarradseers.com>

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.

Keywords

FAQs

Last updated on 08 Aug 2012

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc