'use strict'; var express = require('express'); var async = require('async'); var path = require('path'); var assets = require('../lib/node-ff-assets'); var assetsConfig = { // Map from uri paths to file systems paths. linkMapping : { '/static/module/bootstrap/': 'node_modules/bootstrap/dist/', '/static/module/flot/': 'node_modules/flot/', '/static/': 'www_static/', }, linkSourcesCss : [ '/static/module/bootstrap/css/bootstrap.css', ], linkSourcesJs : [ '/static/js/lib/jquery-2.1.3/jquery.js@http://code.jquery.com/jquery-2.1.3.js', '/static/module/bootstrap/js/bootstrap.js', '/static/module/flot/jquery.flot.js', '/static/module/flot/jquery.flot.resize.js', '/static/module/flot/jquery.flot.pie.js', '/static/js/lib/angularjs-1.4.0-b4/angular.js@https://code.angularjs.org/1.4.0-beta.4/angular.js', '/static/js/lib/angularjs-1.4.0-b4/angular-route.js@https://code.angularjs.org/1.4.0-beta.4/angular-route.js', '/static/js/lib/angularjs-1.4.0-b4/angular-resource.js@https://code.angularjs.org/1.4.0-beta.4/angular-resource.js', '/static/js/lib/angularjs-1.4.0-b4/angular-touch.js@https://code.angularjs.org/1.4.0-beta.4/angular-touch.js', ], }; //callback helper to return an assembler config per asset type function createBuildConfig(type) { return function(callback) { var singleResult = 'false' !== process.env.DEV_ASSETS_SINGLE_RESULT; console.info('Asset single page result: '+singleResult+' for: '+type); if (type === 'css') { callback(null,{ linkMapping: assetsConfig.linkMapping, linkTargetSingleResult: singleResult, linkTarget: '/static/css/lib/assets.css', linkSources: assetsConfig.linkSourcesCss, assetType: type, }); } else { callback(null, { linkTargetSingleResult: singleResult, linkMapping: assetsConfig.linkMapping, linkTarget: '/static/js/lib/assets.js', linkSources: assetsConfig.linkSourcesJs, assetType: type, }); } }; } function buildAssets(server,callbackDone) { assets.build({ assets: { js: { configCreate: createBuildConfig('js'), configFill: function (config, callback) { async.series([ assets.factory.lib.async.pushLinkSources(config, '/static/js/', 'www_static/js/'), assets.factory.lib.async.pushLinkSources(config, '/static/js/controller/', 'www_static/js/controller/'), ],callback); }, }, css: { configCreate: createBuildConfig('css'), configFill: function (config, callback) { async.series([ assets.factory.lib.async.pushLinkSources(config, '/static/css/', 'www_static/css/'), ],callback); }, assemblerCreate: function (assemblerConfig, callback) { callback(null,new assets.AssetAssembler(assemblerConfig,assets.factory.assembler.constructor.readFileMinify())); }, }, }, assemblerFill: function (assembler, callback) { var serverResultKey = 'ff_assets_'+assembler.config.assetType; assembler.on ('log', assets.factory.assembler.event.log.console(assembler.config.assetType)); assembler.on ('result', assets.factory.assembler.event.result.objectSet(server,serverResultKey)); callback(); }, },callbackDone); } function renderTemplate() { return function (req, res) { if (req.params.sub) { res.render('thtml/' + req.params.page + '/' + req.params.sub); } else { res.render('thtml/' + req.params.page); } }; } function renderPage(server) { return function (req, res) { res.render('index', { pageTitle: 'node-ff-assets example', pageKeywords: 'node,ff,assets,example,ui', pageCssFiles: server.get('ff_assets_css'), pageJsFiles: server.get('ff_assets_js'), }); }; } function sendRedirect() { return function (req, res) { res.redirect('/example-ui'); }; } var server = express(); buildAssets(server,function(err) { if (err) { throw err; } console.info('Server assets done.'); server.set('view engine', 'ejs'); server.set('views', path.join(__dirname, 'www_views')); server.use('/static',express.static(path.join(__dirname,'www_static'))); server.use('/static/module/bootstrap',express.static(path.join(__dirname,'node_modules/bootstrap/dist'))); server.use('/static/module/flot',express.static(path.join(__dirname,'node_modules/flot'))); server.get('/example-ui/thtml/:page', renderTemplate()); server.get('/example-ui/thtml/:page/:sub', renderTemplate()); server.get('/', sendRedirect()); server.get('/example-ui', renderPage(server)); server.get('/example-ui/*', renderPage(server)); // must be last; for HTML5 history console.info('Server config done.'); server.listen(8080); console.info('Server started on port 8080'); }); module.exports = { version: 'example' };