Created
September 16, 2017 19:27
-
-
Save derrandz/d15b1d1c11a8191b03188b71bfb85ac8 to your computer and use it in GitHub Desktop.
Old snippet I used to play with when I had to route assets manually back then
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
var Path = require('path'); | |
var express = require('express'); | |
var router = express.Router(); | |
function render(_fn, path, param, req, res, next) { | |
if (req.params[param]) { | |
res[_fn].call(res, Path.join(...[global.appRoot].concat(path(req.params[param]).split("/")))); | |
}else { | |
next(); | |
} | |
} | |
/** | |
* Component's directive's templates | |
*/ | |
let boundComponentTemplateRouter = render.bind(null, 'render', (templateName) => "public/app/components/templates/" + templateName, 'templateName'); | |
router.get("/components/directives/templates/:templateName", boundComponentTemplateRouter); | |
/** | |
* Shared directive's templates | |
*/ | |
let boundSharedTemplateRouter = render.bind(null, 'render', (templateName) => "public/app/shared/templates/" + templateName, 'templateName'); | |
router.get("/shared/directives/templates/:templateName", boundSharedTemplateRouter); | |
/** | |
* Component's Views | |
*/ | |
let boundComponentViewRouter = render.bind(null, 'render', (viewName) => "public/app/components/views/" + viewName, 'viewName'); | |
router.get("/components/views/:viewName", boundComponentViewRouter); | |
/** | |
* Load any node_module | |
*/ | |
router.get('/node_module/:moduleName/*', function(req, res, next) { | |
render('sendFile',(moduleName) => "node_modules/" + moduleName + "/" + req.params['0'], 'moduleName', req, res, next); | |
}); | |
/** | |
* Load any 'angular_module' | |
*/ | |
router.get('/angular_module/:moduleName/*', function(req, res, next) { | |
render('sendFile', (moduleName) => "public/app/assets/vendor/" + moduleName + "/" + req.params['0'], 'moduleName', req, res, next); | |
}); | |
/* GET users listing. */ | |
router.get('*', function(req, res, next) { | |
res.render(Path.join(__dirname, '..', 'public', 'app', 'app')); | |
}); | |
module.exports = router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment