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

@aufentic/express-subdomain

v1.0.7

Published

Simple and lightweight middleware to handle subdomains

Downloads

2

Readme

Maintaining

Although pull requests and issues are welcome, the Aufentic team will keep this package and repository up to date.

Original repository thanks to bmullan91.

express-subdomain

Is simply express middleware. In the examples below I am using Express v4.x.

Install

With npm, saving it as a dependency.

npm i @aufentic/express-subdomain --save

Support for ES modules

import subdomain from 'express-subdomain';
import express from 'express';
var app = express();

// *** Code examples below go here! ***

// example.com
app.get('/', function(req, res) {
    res.send('Homepage');
});

API Router

var router = express.Router();

//api specific routes
router.get('/', function(req, res) {
    res.send('Welcome to our API!');
});

router.get('/users', function(req, res) {
    res.json([
        { name: "Brian" }
    ]);
});

Now register the subdomain middleware:

app.use(subdomain('api', router));
app.listen(3000);

The API is alive:

http://api.example.com/ --> "Welcome to our API!"

http://api.example.com/users --> "[{"name":"Brian"}]"

Simple usage

Let's say you want to provide a RESTful API via the url http://api.example.com

Express boilerplate code:

var subdomain = require('express-subdomain');
var express = require('express');
var app = express();

// *** Code examples below go here! ***

// example.com
app.get('/', function(req, res) {
    res.send('Homepage');
});

API Router

var router = express.Router();

//api specific routes
router.get('/', function(req, res) {
    res.send('Welcome to our API!');
});

router.get('/users', function(req, res) {
    res.json([
        { name: "Brian" }
    ]);
});

Now register the subdomain middleware:

app.use(subdomain('api', router));
app.listen(3000);

The API is alive:

http://api.example.com/ --> "Welcome to our API!"

http://api.example.com/users --> "[{"name":"Brian"}]"

Multi-level Subdomains

app.use(subdomain('v1.api', router)); //using the same router

http://v1.api.example.com/ --> "Welcome to our API!"

http://v1.api.example.com/users --> "[{"name":"Brian"}]"

Wildcards

Say you wanted to ensure that the user has an API key before getting access to it... and this is across all versions.

Note: In the example below, the passed function to subdomain can be just a pure piece of middleware.

var checkUser = subdomain('*.api', function(req, res, next) {
    if(!req.session.user.valid) {
        return res.send('Permission denied.');
    }
    next();
});

app.use(checkUser);

This can be used in tandem with the examples above.

Note: The order in which the calls to app.use() is very important. Read more about it here.

app.use(checkUser);
app.use(subdomain('v1.api', router));

Divide and Conquer

The subdomains can also be chained, for example to achieve the same behaviour as above:

var router = express.Router(); //main api router
var v1Routes = express.Router();
var v2Routes = express.Router();

v1Routes.get('/', function(req, res) {
    res.send('API - version 1');
});
v2Routes.get('/', function(req, res) {
    res.send('API - version 2');
});

var checkUser = function(req, res, next) {
    if(!req.session.user.valid) {
        return res.send('Permission denied.');
    }
    next();
};

//the api middleware flow
router.use(checkUser);
router.use(subdomain('*.v1', v1Routes));
router.use(subdomain('*.v2', v2Routes));

//basic routing..
router.get('/', function(req, res) {
    res.send('Welcome to the API!');
});

//attach the api
app.use(subdomain('api', router));
app.listen(3000);

Invalid user

http://api.example.com/ --> Permission denied.

Valid user

http://api.example.com/ --> Welcome to the API!

http://v1.api.example.com/ --> API - version 1

http://abc.v1.api.example.com/ --> API - version 1

http://v2.api.example.com/ --> API - version 2

http://abc.v2.api.example.com/ --> API - version 2

Developing Locally

If you plan to use this middleware while developing locally, you'll have to ensure that your subdomain is listed in your hosts file.

On Linux or OSX, add your subdomain to /etc/hosts:

127.0.0.1       myapp.internal
127.0.0.1       subdomain.myapp.internal

You may not have write permissions on your hosts file, in which case you can grant them:

$ sudo chmod a+rw /etc/hosts

Note: Express parses the request URL for a top level domain, so developing locally without one won't be possible because Express will treat the subdomain as the domain, and the actual domain as a TLD.

Windows

On Windows 7 and 8, the hosts file path is %systemroot%\system32\drivers\etc.

Gotchas

Multilevel TLD's, such as .co.uk you have to pass api.example as the subdomain:

app.use(subdomain('api.example', router));

See https://github.com/bmullan91/express-subdomain/issues/17 for more info.