149 lines
4.8 KiB
JavaScript
149 lines
4.8 KiB
JavaScript
'use strict';
|
|
|
|
var express = require('express');
|
|
var path = require('path');
|
|
var fs = require('fs');
|
|
var minify = require('minify');
|
|
var fetch = require('fetch');
|
|
var cors = require('cors');
|
|
var morgan = require('morgan')
|
|
|
|
// example options;
|
|
var serverUrl = 'http://localhost:8080';
|
|
var useInline = true; // or false
|
|
|
|
var clientResourcesWeb = [];
|
|
var clientResources = {
|
|
js: [],
|
|
css: [],
|
|
cssData: []
|
|
}
|
|
|
|
var addClientResource = function(clientResource, resourceType) {
|
|
clientResources[resourceType].push(clientResource);
|
|
}
|
|
|
|
var stringHash = function (str) {
|
|
var hash = 31; // prime
|
|
for (var i = 0; i < str.length; i++) {
|
|
hash = ((hash<<5)-hash)+str.charCodeAt(i);
|
|
hash = hash & hash; // keep 32b
|
|
}
|
|
return hash;
|
|
};
|
|
|
|
var fetchHashResource = function(fetchEntry,cb) {
|
|
fetch.fetchUrl(serverUrl + fetchEntry.url,function(err, meta, data) {
|
|
if (err !== null) { return cb(err); }
|
|
var assetHash = stringHash(''+data);
|
|
clientResourcesWeb.push({
|
|
url: fetchEntry.url,
|
|
type: fetchEntry.type,
|
|
hash: assetHash
|
|
});
|
|
cb(null);
|
|
});
|
|
};
|
|
|
|
var fetchHashResources = function(fetchList, cb) {
|
|
var resourceStack = fetchList;
|
|
var resourceLoader = function() {
|
|
resourceStack = resourceStack.slice(1);
|
|
if (resourceStack.length === 0) {
|
|
cb(null);
|
|
} else {
|
|
fetchHashResource(resourceStack[0],resourceLoader);
|
|
}
|
|
};
|
|
fetchHashResource(resourceStack[0],resourceLoader);
|
|
};
|
|
|
|
var createClientResourceFetchList = function() {
|
|
var fetchList = [];
|
|
for (var clientResourceIdx in clientResources.js) {
|
|
var url = clientResources.js[clientResourceIdx];
|
|
fetchList.push({url:url,type:'js'});
|
|
}
|
|
for (var clientResourceIdx in clientResources.css) {
|
|
var url = clientResources.css[clientResourceIdx];
|
|
fetchList.push({url:url,type:'css'});
|
|
}
|
|
for (var clientResourceIdx in clientResources.cssData) {
|
|
var url = clientResources.cssData[clientResourceIdx];
|
|
fetchList.push({url:url,type:'cssData'});
|
|
}
|
|
return fetchList;
|
|
};
|
|
|
|
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 renderIndex(server) {
|
|
var inline = '';
|
|
minify(__dirname+'/../es5-ff-spa-loader.js', {}, function(err, data) {
|
|
inline = '\n\t\t<script>'+data+'</script>';
|
|
});
|
|
var inlineNot = '\n\t\t<script src=\"/static/es5-ff-spa-loader.js\"></script>';
|
|
return function (req, res) {
|
|
res.render('index', {
|
|
inlineNot: useInline?'':inlineNot,
|
|
inline: useInline?inline:''
|
|
});
|
|
};
|
|
}
|
|
|
|
addClientResource('/static/module/jquery/jquery.js','js');
|
|
addClientResource('/static/module/angular/angular.js','js');
|
|
addClientResource('/static/module/angular-route/angular-route.js','js');
|
|
addClientResource('/static/module/bootstrap/css/bootstrap.css','css');
|
|
addClientResource('/static/module/bootstrap/js/bootstrap.js','js');
|
|
addClientResource('/static/css/boot.css','css');
|
|
addClientResource('/static/css/style.css','css');
|
|
addClientResource('/static/js/example-app.js','js');
|
|
addClientResource('/static/js/controller/page-bar.js','js');
|
|
addClientResource('/static/js/controller/page-foo.js','js');
|
|
addClientResource('/static/js/controller/page-index.js','js');
|
|
|
|
var server = express();
|
|
server.use(morgan('dev'))
|
|
server.use(cors({credentials: true, origin: '*'}));
|
|
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/jquery', express.static(path.join(__dirname,'node_modules/jquery/dist')));
|
|
server.use('/static/module/angular', express.static(path.join(__dirname,'node_modules/angular')));
|
|
server.use('/static/module/angular-route', express.static(path.join(__dirname,'node_modules/angular-route')));
|
|
|
|
server.get('/static/es5-ff-spa-loader.js', function (req,res) {
|
|
res.write(fs.readFileSync(__dirname+'/../es5-ff-spa-loader.js', 'utf8'));
|
|
res.end();
|
|
});
|
|
server.get('/static/spa-client-resources', function (req,res) {
|
|
res.json({data: {resources: clientResourcesWeb}});
|
|
});
|
|
|
|
server.get('/', function (req, res) {res.redirect('/example-ui');});
|
|
server.get('/example-ui/thtml/*', renderTemplatePath('thtml/'));
|
|
server.get('/example-ui', renderIndex(server));
|
|
server.get('/example-ui/*', renderIndex(server)); // must be last; for HTML5 history
|
|
console.info('Server config done.');
|
|
|
|
server.listen(8080);
|
|
console.info('Server started on port 8080');
|
|
|
|
var res = createClientResourceFetchList();
|
|
fetchHashResources(res, function(err) {
|
|
if (err !== null) {console.log(err);};
|
|
console.log('Total assets build: '+clientResourcesWeb.length);
|
|
});
|
|
|