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

express-vhosts-autoloader

v1.1.3

Published

Autoloads express middlewares using the hostname as the module foldername.

Downloads

19

Readme

NPM Version NPM Downloads Codacy Badge Build Status Test Coverage License

Express VHosts Autoloader

This module helps you create an Express JS server with virtual hosts auto and manual management.

Install

npm install express-vhosts-autoloader

Usage

This module autoloads your express app's as express middlewares when their directories matches the domain name.

Your directory names must be the same as your domain names in order for this module to work.

Let's assume you have this directory structure :

My Server Directory :

  • server.js

  • node_modules

    • express
    • express-vhosts-autoloader
  • www.nodeapp1.com

    • app.js
  • www.nodeapp2.com

    • app.js
  • www.nodeapp3.com

    • app.js

And your domain names (pointing to your server) are :

  • www.nodeapp1.com
  • www.nodeapp2.com
  • www.nodeapp3.com
// Load express.
var express = require("express");

// Load express vhosts autoloader.
var vhostsAutoloader = require("express-vhosts-autoloader");

// Create express server.
var expressServer = express();

// Trigger vhostsAutoloader with expressServer as parameter.
vhostsAutoloader(expressServer);

// Start your express web server
var port = process.env["PORT"] || 80;
var server = expressServer.listen(port, function () {
  console.log("Server listening on port %d ", port);
});

It works !

The express vhosts autoloader will load each app.js module in each directory as an express middleware triggered only when the required host (i.e domain name) is provided.

Each app.js middleware should end with something like module.exports.app = app or exports.app = app

API

Promise <= vhostsAutoloader(expressServer, options)

This function tries to load any app.js file in any directory in the server root directory as an express middleware trigger only when the required domain name is provider in the request.

  • expressServer (object, required) express server instance.

  • options : (object, optional).

    • options.debug (boolean, optional) : defaults to false. If true makes the module more verbose in the console.
    • options.directory (string, optional) : defaults to server root directory. If set vhostsAutoloader tries to load any app.js from the directory provided.
    • options.ignoreList (array, optional) : defaults to [".git",".vscode",".circleci",".nyc_output","log","logs","test","tests","coverage","node_modules"]

Example

// Tries to load any file in directories located inside /home/user
vhostsAutoloader(expressServer, {
  directory: "/home/user",
});

Promise <= vhostsAutoloader.loadVhost(options)

This utility method loads an express middleware triggered only when the required host (i.e domain name) is provided.

  • options : (object, required).
    • options.debug (boolean) : defaults to false. If true makes the module more verbose in the console.
    • options.domainName (string, required) : the domain name / directory name
    • options.mainFile (string, optional) : defaults to 'app'. If set the method will try to load the file named after the provided value.
    • options.exportsProperty (string, optional) : defaults to 'app'. If set the method will try to use the exports property named after the provided value
    • options.expressServer (object, optional | required) : Optional if used after calling vhostsAutoloader. Required if vhostsAutoloader.loadVhost is used alone.
    • options.directory (object, optional) : defaults to server root directory. If set vhostsAutoloader.loadVhost tries to load the module from the directory\domainName directory.

Examples

Used after calling vhostsAutoloader

vhostsAutoloader.loadVhost({
  domainName: "www.foo.com",
});

For www.foo.com directory .\www.foo.com will be served and app.js module required, needs module.exports.app to be set in app.js.

vhostsAutoloader.loadVhost({
  domainName: "www.foobar.com",
  mainFile: "index",
});

For www.foobar.com directory .\www.foobar.com will be served and index.js module required, needs module.exports.index to be set in index.js.

vhostsAutoloader.loadVhost({
  domainName: "www.foobarfoo.com",
  mainFile: "index",
  exportsProperty: "bar",
});

For www.foobarfoo.com directory .\www.foobarfoo.com will be served, index.js module required, needs module.exports.bar to be set in index.js.

Used without calling vhostsAutoloader

// Load express.
var express = require("express");
// Load express vhosts autoloader.
var vhostsAutoloader = require("express-vhosts-autoloader");
// Create express server.
var expressServer = express();

vhostsAutoloader.loadVhost({
  domainName: "www.foobarfoo.com",
  mainFile: "foo",
  exportsProperty: "bar",
  expressServer: expressServer,
});

For www.foobarfoo.com directory .\www.foobarfoo.com will be served, foo.js module required, needs module.exports.bar to be set in foo.js.

License

MIT