2
0
Fork 0
node-ff-assets/example/example.js

97 lines
3.1 KiB
JavaScript

'use strict';
var express = require('express');
var async = require('async');
var path = require('path');
var assets = require('../lib/node-ff-assets');
function buildAssets(server,callbackDone) {
var singleResult = 'false' !== process.env.DEV_ASSETS_SINGLE_RESULT;
var assetsConfig = require('./example-assets.json');
assets.build({
assets: {
js: {
configCreate: assets.factory.builder.configCreate.fromJSON(assetsConfig,'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: assets.factory.builder.configCreate.fromJSON(assetsConfig,'css'),
configFill: function (config, callback) {
async.series([
assets.factory.lib.async.pushLinkSources(config, '/static/css/', 'www_static/css/'),
],callback);
},
},
},
assemblerCreate: assets.factory.builder.assemblerCreate.readFileRegex(),
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));
assembler.config.linkTargetSingleResult = singleResult;
callback();
},
},callbackDone);
}
function renderTemplatePath(viewPath) {
return function (req, res) {
res.locals.query = req.query;
var qi = req.url.indexOf('?');
if (qi === -1) {
qi = req.url.length;
}
res.render(viewPath + req.url.substring(req.route.path.length-1, qi));
};
}
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('/', sendRedirect());
server.get('/example-ui/thtml/*', renderTemplatePath('thtml/'));
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'
};