Giter Site home page Giter Site logo

express-subdomain's Introduction

Build Status Coverage Status

#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 express-subdomain --save

##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

##Need in-depth examples?

Have a look at the tests!

express-subdomain's People

Contributors

bmullan91 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.