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

subdomain-router-middleware

v1.0.0

Published

Middleware for handling subdomains using Express and React

Downloads

7

Readme

subdomain-router-middleware

Middleware for handling subdomains using Express.

subdomain-router-middleware provides validation on express routes so that they are only accessible via the correct subdomain.

Installation

npm install subdomain-router-middleware

Dependencies

  • Express JS
  • property-validator

Usage

  1. In the router file of your express project, require the package as follows.
var subdomain = require("subdomain-router-middleware");

Certain initialisation parameters need to be declared in order for the middleware to function correctly.

  1. Create an object with the following parameters and pass it into the init function.
subdomain.init({
  asyncLoad: false,
  asyncFunc: null,
  error: {
    success: false,
    message: "Invalid subdomain"
  }
});

| Parameter | Type | Description | |:---|:---|:---| | asyncLoad | bool | Boolean to specify if valid subdomains should be asynchronously loaded. | | asyncFunc | Function | Specify a promise function to asynchronously load a list of valid subdomains. | | error | JSON | JSON that is returned when a subdomain is invalid. |

In your express routes, add the middleware.

router.get("/test", subdomain.route("subdomain"), function(req, res) {}

Validate individual subdomain

To specifiy a route that is only accessible with a certain subdomain, add the middleware and pass a string that contains the valid subdomain.

subdomain.route("mysubdomain")

Load valid subdomains asyncronously

In many web applications, users can specify their own subdomain linked to their account.

You may need to asyncronously load a list of valid subdomains where the route is accessible.

To do so, we first need to modify the setup object we passed into the subdomain.init() function.

  1. Change the boolean parameter asyncLoad to true.

  2. Add a function to the asyncFunc parameter.

asyncFunc: function() {
    return new Promise((resolve, reject) => {
      // Server call to retrieve subdomains
      const array = [];
      resolve(array);
    });
  },

The middleware expects a javascript promise that returns an array containing all of the subdomains that are valid for the route.

Invalid routes with subdomain

If you want certain routes to be completely inaccessible via a subdomain, you can use the alternative subdomain middleware function called root.

In your route, add the function.

router.get("/test", subdomain.root(), function(req, res) {}

This route will no longer be valid when accessed through a subdomain.

Subdomains in development mode

If you need to test and develop locally, it is still possible to use this package.

When NODE_ENV is set to development, an alternative regex is used that handles subdomains on localhost. More complex regex is used on production environments to determine the correct subdomain.

Set the NODE_ENV environment to development.

process.env.NODE_ENV = "development"

The chrome web browser supports using subdomains on localhost. You can test locally as follows:

http://subdomain.localhost:8080/

Router Example

var express = require("express");
var path = require("path");
var subdomain = require("subdomain-router-middleware");

// Define our express router object
let router = express.Router();

// Initialise default subdomain parameters
subdomain.init({
  asyncLoad: true,
  asyncFunc: function() {
    return new Promise((resolve, reject) => {
      // Server call to retrieve subdomain for security token
      const array = ["subdomain1", "subdomain2", "subdomain3"];
      resolve(array);
    });
  },
  error: {
    success: false,
    message: "Invalid subdomain"
  }
});

// Express Route
router.get("/test", subdomain.route(), function(req, res) {
  res.json({
    success: true,
    message: "Endpoint loaded successfully"
  });
});

module.exports = router;