Skip to content

Commit

Permalink
Merge pull request #118 from Kanaye/blocks.middleware
Browse files Browse the repository at this point in the history
Added Blocks.middleware...
  • Loading branch information
astoilkov committed Dec 11, 2015
2 parents 9bd1f1d + 54b6ecc commit 1123d4f
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 6 deletions.
22 changes: 21 additions & 1 deletion src/modules/Router.js
Expand Up @@ -116,6 +116,7 @@
function Router() {
this._currentRoute = {};
this._routes = {};
this._baseUrl = '';
}

blocks.core.Router = Router;
Expand Down Expand Up @@ -170,6 +171,10 @@
}
});

if (this._baseUrl) {
result = this._baseUrl + result;
}

return result;
},

Expand All @@ -180,6 +185,10 @@

url = decodeURI(url);

if (this._baseUrl && url.startsWith(this._baseUrl)) {
url = url.substr(this._baseUrl.length, url.length);
}

blocks.each(this._routes, function (routeMetadata) {
blocks.each(routeMetadata.regExCollection, function (regEx) {
if (regEx.regEx.test(url)) {
Expand All @@ -189,7 +198,7 @@
id: routeMetadata.route._routeString,
params: getUrlParams(routeMetadata, regEx.params, matches)
});
routeMetadata = routeMetadata.parent;
routeMetadata = routeMetadata.parent;
}
return false;
}
Expand Down Expand Up @@ -380,6 +389,17 @@
});
}
});
},
_setBaseUrl: function (url) {
if (blocks.isString(url)) {
if (!url.endsWith('/')) {
url += '/';
}
if(url.startsWith('/')) {
url = url.substr(1, url.length);
}
this._baseUrl = url;
}
}
};

Expand Down
5 changes: 5 additions & 0 deletions src/mvc/Application.js
Expand Up @@ -286,6 +286,11 @@ define([
if (!this._started) {
this._started = true;
this._serverData = window.__blocksServerData__;

if (this._serverData && this._serverData.baseUrl) {
this._router._setBaseUrl(this._serverData.baseUrl);
}

this._createViews();
blocks.domReady(blocks.bind(this._ready, this, element));
}
Expand Down
6 changes: 5 additions & 1 deletion src/node/BrowserEnv.js
Expand Up @@ -28,7 +28,11 @@ define([
location[name] = props[name];
});
},

setBaseUrl: function (url) {
if (url) {
this._env.__baseUrl__ = url;
}
},
addElementsById: function (elementsById) {
var env = this._env;
env.document.__elementsById__ = elementsById;
Expand Down
7 changes: 5 additions & 2 deletions src/node/Middleware.js
Expand Up @@ -29,7 +29,8 @@ define([
Middleware.Defaults = {
static: 'app',
blocksPath: 'node_modues/blocks/blocks.js',
cache: true
cache: true,
baseTag: false
};

Middleware.prototype = {
Expand All @@ -39,6 +40,7 @@ define([
next();
} else {
res.send(contents);
return;
}
});
},
Expand Down Expand Up @@ -100,6 +102,7 @@ define([
_createBrowserEnv: function (req, server) {
var browserEnv = BrowserEnv.Create();

browserEnv.setBaseUrl(req.baseUrl);
browserEnv.fillLocation(this._getLocation(req));
browserEnv.addElementsById(this._elementsById);
browserEnv.fillServer(server);
Expand All @@ -108,7 +111,7 @@ define([
},

_getLocation: function (req) {
return req.protocol + '://' + req.get('host') + req.url;
return req.protocol + '://' + req.get('host') + req.originalUrl;
}
};
});
1 change: 1 addition & 0 deletions src/node/createBrowserEnvObject.js
Expand Up @@ -31,6 +31,7 @@ define(function () {
var timeoutId = 0;
var windowObj = {
__mock__: true,
__baseUrl__: '',

console: createConsoleMock(),

Expand Down
41 changes: 39 additions & 2 deletions src/node/methods.js
@@ -1,12 +1,49 @@
define([
'../core',
'./Server',
], function (blocks, Server) {
'./Middleware'
], function (blocks, Server, Middleware) {
blocks.server = function (options) {
return new Server(options);
};

blocks.static = function (options) {


};

blocks.middleware = function (options) {
var express = require('express');
var path = require('path');
// the real middleware
var middleware;
// array of middlewares to return
var middlewares = [];

var defaults = {
// Should the files in static get delivered
deliverStatics: true,
baseTag: true
};

if (blocks.isString(options)) {
options = {
static: options
};
}

options = blocks.extend({}, Middleware.Defaults, defaults, options);
middleware = new Middleware(options);

if (options.deliverStatics) {
// express.static is required as the frontend app needs it's files
middlewares.push(express.static(path.resolve(options.static), {
index: false
}));
}

middlewares.push(blocks.bind(middleware.tryServePage, middleware));

// returning array of express middlewares that express will call in that order
return middlewares;
};
});
18 changes: 18 additions & 0 deletions src/node/overrides.js
Expand Up @@ -49,6 +49,9 @@ define([
server.await(function () {
if (head) {
head.children().splice(0, 0, getServerDataScript());
if (server.options.baseTag) {
head.children().splice(0, 0, getBaseTag());
}
}
server.rendered = root.renderChildren();
});
Expand All @@ -71,6 +74,11 @@ define([
return result;
}

function getBaseTag() {
var baseUrl = window.location.protocol + '//' + window.location.host + window.__baseUrl__ + '/';
return VirtualElement('base').attr('href', baseUrl);
}

function getServerDataScript() {
return VirtualElement('script').html('window.__blocksServerData__ = ' + JSON.stringify(server.data)).render();
}
Expand Down Expand Up @@ -198,4 +206,14 @@ define([
this.callSuccess(contents);
}
};

var blocksApplication = blocks.Application;

blocks.Application = function (options) {
var app = blocksApplication(options);
app._router._setBaseUrl(window.__baseUrl__);
server.data.baseUrl = window.__baseUrl__;
return app;
};

});

0 comments on commit 1123d4f

Please sign in to comment.