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

km-express-load

v1.2.0

Published

Autoload modules into an Express application instance, config, models, routes, controllers etc...

Downloads

8

Readme

Express Load

NOTE:

The successor to this module is here: consign it's not express specific and is lighter in weight. Check it out :)

The express-load module provides the ability to load scripts into an Express instance from specified directories or files. Make large express MVC applications easier to develop by allowing a logical file separation without having to require your scripts. See the examples folder for information.

Express Load can be used to autoload models, routes, schemas, configs, controllers, object maps... etc...

You get 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. req.app

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

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

load('config')
  .then('routes')
  .into(app);

Simple Express Load Example

controllers/site.js

exports.index = function(req, res, next) {
  res.send('Hello world!');
};

routes/site.js

module.exports = function(app) {

  var site = app.controllers.site;
  
  app.get('/',
    site.index
  );

};

app.js

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

var app = express();

load('controllers')
  .then('routes')
  .into(app);

app.listen(3000)

Load Order

The basic load order is the order that is specified in code, for example, you will want to load models before controllers and controllers before routes:

load('models')
  .then('controllers')
  .then('routes')
  .into(app);

This would simply load all models, all controllers, all routes in that order (alphabetical order within the directories).

Specifying Complex Order

If you have two files in the foo folder a.js and z.js and you need z.js loaded BEFORE a.js you would simply do the following:

load('foo/z.js').then('foo').into(app);

express-load will recognise the order and will not add it again later down the chain.

Async load

If you have an async script to load, express-load will pass a callback function to your script and wait this function be called to load remain scripts.

load('syncFoo.js').then('asyncBar.js').then('syncBar.js');

// asyncBar.js
module.exports = function (app, callback) {
    setTimeout(function () {
        console.log('After 5 seconds');
        callback();
    }, 5000);
};

If you need to know when async load is complete, you may pass the callback function as a second argument to into. The callback get called when async load is completed.

load('models')
    .then('collections')
    .then('controllers')
    .into(app, function(err, instance) {
        if(err) throw err;
        app.listen(app.get('port'));
    });

Logging

Logging is off by default but can be enabled in the following way:

load('controllers', {verbose:true}).into(app);

See the verbose example in the examples folder.

Files and folders

express-load will ignore hidden files and folders (by leading period) unless you explicitly define them to be loaded.

express-load will by default only load files ending with .js, .node, .coffee, .sjs, .json extensions, unless you set checkext option to false : no file extension check at all. You can also change the list of allowed extension (in extlist option).

load('controllers', {checkext:true, extlist:['.js','.myextension']}).into(app);

Nested Folders

If you had nested folders like the following example:

models
	humans
		cool.js
		not.js
	animals
		dog.js
		cat.js

You would end up with the scripts being available in the following structure:

app.models.humans.cool
app.models.humans.not
app.models.animals.dog
app.models.animals.cat

Base directory

Express-load load scripts based on relative directory, however you can use cwd option if you want to load based on the other directory. see example.

Getting the Express Application instance

This can be done in one of two ways:

In a script that is auto-loaded...

module.exports = function(app) {
  console.log(app);
};

A script will only be loaded with parameters if module.exports is a function. Multiple parameters can be passed to the script in the into method, for example:

load('controllers').into(app, parameter, another);

From a request object...

app.get('/', function(req, res, next) {
  console.log(req.app);
});

File and Folder names vs Object namespace examples

The names of files and folders are used to create the namespace therefore rules have been put in place. dashes - and periods . are removed and the following character is uppercased. For example:

some-controllers/my.controller.js -> *.someControllers.myController

Please see the examples folder for working examples of express-load in action.

License

(The MIT License)

Copyright (c) 2012 Jarrad Seers <[email protected]>

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.