Tuesday, 2 May 2017

Switch between routers Express

I want to display a website completely different in function of an arbitrary value.

Let's say I have two routers

const express = require('express');
const app = express();
const router1 = express.Router();
router1.get('/', (req, res, next) => res.json({message: 'I am the router1'}))
const router2 = express.Router();
router2.get('/', (req, res, next) => res.json({message: 'I am the router2'}))
app.use((req, res, next) => {
    if(Math.random() > 0.5) {
        // Use router1
    } else {
        // Use router2
    }
})

I have no idea how I can do that. I will have a lots of routes (router.get, router.post) I don't want to check that on each route

Thanks



via ant

No comments:

Post a Comment