Last active
January 19, 2017 06:58
-
-
Save Steinweber/d4dd1351bbfc8e9159334bcae4da1f2b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const router = require('restify-router').Router; | |
const routes = new router(); | |
const AuthController = require('../controller/auth'); | |
routes.post('/login',AuthController.login); | |
routes.get('/logout',AuthController.logout); | |
routes.get('/session',AuthController.session); | |
//return the restify-router instance | |
module.exports = routes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const router = require('restify-router').Router; | |
const routes = new router(); | |
const CartController = require('../controller/cart'); | |
routes.get('/',CartController.getCart); | |
routes.post('/addProduct',CartController.addProduct); | |
routes.delete('/removeProduct',CartController.removeProduct); | |
//return the restify-router instance | |
module.exports = routes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//The index.js should not have much logic. Only build the version of the API and assign controller. | |
const router = require('restify-router').Router; | |
//returns restify-router instance | |
const product = require('./product'); | |
const cart = require('./cart'); | |
const auth = require('./auth'); | |
module.exports = (app) => { | |
//setup a router for v1 & v2 and | |
//add all controllers that are the same in both versions | |
const basicRouter = new router(); | |
//auth and product are the same in v1 & v2 | |
basicRouter.add('/auth', auth); | |
basicRouter.add('/product', product); | |
//setup the v1 | |
const apiV1 = new router(); | |
//copy/merge basic router to v1 | |
apiV1.merge(basicRouter); | |
//Add additional controllers to v1 | |
//These controllers are only available in v1 | |
apiV1.get('/foo', foo); | |
//bind apiV1 to app/server and set prefix | |
apiV1.applyRoutes(app, '/api/v1'); | |
// GET /api/v1/product/:id | |
// GET /api/v1/cart/ -> notFound / it´s a v2 restricted endpoint | |
//setup apiV2 | |
const apiV2 = new router(); | |
//merge/copy basic router as in v1 | |
apiV2.merge(basicRouter); | |
//Add additional controllers to v2 | |
//These controllers are only available in v2 (not in v1) | |
apiV2.add('/cart',cart); | |
apiV2.get('/foo', bar); | |
//bind v2 to app/server and set prefix | |
apiV2.applyRoutes(app, '/api/v2'); | |
// GET /api/v2/cart/ | |
// POST /api/v2/auth/login -> same as POST /api/v1/auth/login | |
//setup v3 | |
const apiV3 = new router(); | |
//v3 is v1 + v2 | |
//last set overwrite existing method | |
apiV3.merge(apiV1,apiV2); | |
//v1 & v2 have POST /auth/login | |
//merge apiV1 will set first POST /auth/login and be overwritten by apiV2 with the same controller | |
//v1 & v2 have GET /foo but different controller (v1/foo | v2/bar) | |
//By merging v2 after v1, GET /foo will return the v2/bar controller, because this has overwritten the v1/foo controller | |
//bind v3 to app/server and set prefix | |
apiV3.applyRoutes(app, '/api/v3'); | |
// GET /api/v3/cart/ the v2 restricted endpoint in v3 | |
// POST /api/v3/auth/login -> same as POST /api/v2/auth/login -> same as POST /api/v1/auth/login | |
// GET /api/v3/foo -> same as /api/v2/foo -> NOT same as ->/api/v1/foo (v1 replaced by v2) | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const router = require('restify-router').Router; | |
const routes = new router(); | |
const ProductController = require('../controller/product'); | |
routes.get('/:id',ProductController.getProduct); | |
routes.post('/add',ProductController.addProduct); | |
//return the restify-router instance | |
module.exports = routes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//merge is combine existing endpoints with new endpoints | |
//clone JUST copy the router | |
//v1 is a restify-router instance like model-product.js | |
const v1 = require('./v1.0_router'); | |
v1.get('/foo', (req,res,next) => {}); | |
const v1_1 = v1.clone().merge(require('./v1.1_router')); | |
//same as | |
const v1_1 = new router(); | |
v1_1.merge(v1); | |
v1_1.merge(require('./v1.1_router')); | |
//copy all from v1.0 to v1.1 | |
const v1_1 = v1.clone() | |
//add new endpoints from 1.1 and (if in v1.1 also exists) overwrite v1.0 endpoints | |
v1_1.merge(require('./v1.1_router')); | |
//v1_1 new router() not required, because its copied from 1.0 | |
//in short | |
const v1_1 = v1.clone().merge(require('./v1.1_router')); | |
//full | |
const v1_1 = v1.clone().merge(require('./v1.1_router')).applyRoutes(app,'/api/v1.1'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment