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

97 lines
3.1 KiB
JavaScript
Raw Permalink Normal View History

2015-02-27 20:50:19 +00:00
'use strict';
2015-02-27 14:20:01 +00:00
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');
2015-02-27 14:20:01 +00:00
assets.build({
assets: {
js: {
configCreate: assets.factory.builder.configCreate.fromJSON(assetsConfig,'js'),
2015-02-27 14:20:01 +00:00
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'),
2015-02-27 14:20:01 +00:00
configFill: function (config, callback) {
async.series([
assets.factory.lib.async.pushLinkSources(config, '/static/css/', 'www_static/css/'),
2015-02-27 14:20:01 +00:00
],callback);
},
},
},
assemblerCreate: assets.factory.builder.assemblerCreate.readFileRegex(),
2015-02-27 14:20:01 +00:00
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;
2015-02-27 14:20:01 +00:00
callback();
},
},callbackDone);
}
function renderTemplatePath(viewPath) {
2015-02-27 20:50:19 +00:00
return function (req, res) {
res.locals.query = req.query;
var qi = req.url.indexOf('?');
if (qi === -1) {
qi = req.url.length;
2015-02-27 14:20:01 +00:00
}
res.render(viewPath + req.url.substring(req.route.path.length-1, qi));
2015-02-27 20:50:19 +00:00
};
2015-02-27 14:20:01 +00:00
}
function renderPage(server) {
2015-02-27 20:50:19 +00:00
return function (req, res) {
2015-02-27 14:20:01 +00:00
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'),
2015-02-27 14:20:01 +00:00
});
2015-02-27 20:50:19 +00:00
};
2015-02-27 14:20:01 +00:00
}
function sendRedirect() {
2015-02-27 20:50:19 +00:00
return function (req, res) {
2015-02-27 14:20:01 +00:00
res.redirect('/example-ui');
2015-02-27 20:50:19 +00:00
};
2015-02-27 14:20:01 +00:00
}
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')));
2015-02-27 14:20:01 +00:00
server.get('/', sendRedirect());
server.get('/example-ui/thtml/*', renderTemplatePath('thtml/'));
2015-02-27 14:20:01 +00:00
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'
};