Skip to content

Commit

Permalink
[api] [middle] Added additional req/res Closes #1
Browse files Browse the repository at this point in the history
  * Also added stubs for regex routing on 404
  • Loading branch information
Marak committed Apr 19, 2016
1 parent 41dd10c commit 004740b
Showing 1 changed file with 60 additions and 3 deletions.
63 changes: 60 additions & 3 deletions lib/middle.js
Expand Up @@ -4,7 +4,7 @@ var autoindex = require('./autoindex');
module['exports'] = function (options) {
options.prefix = options.prefix || '';

return function (req, res, next) {
return function routeRequest (req, res, next) {
if (options.view) {
//
// If the view was mounted with a prefix and that prefix was not found in the incoming url,
Expand All @@ -14,39 +14,93 @@ module['exports'] = function (options) {
return next();
}
var _view = options.view;

// todo: keep track of level found, if we 404'd in a found level, show last known parent
var parts = require('url').parse(req.url).pathname.replace(options.prefix, '').split('/');
parts.shift();

// Remark: special case for root with no index, should be refactored
if (parts.length === 1 && parts[0] === "" && !_view['index']) {
return missingViewHandler();
}

var previousView;
var foundViews = 0;
parts.forEach(function(part) {
if(part.length > 0 && typeof _view !== 'undefined') {
previousView = _view || _view[part];
_view = _view[part];
foundViews++;
}
});

if (_view && _view['index']) {
_view = _view['index'];
}
if(typeof _view === "undefined") {

/*
// TODO: uncomment out next block
req.resource = req.resource || {};
req.resource._args = parts;
req.resource.args = parts.slice(foundViews - 1, parts.length);
*/

if (typeof _view === "undefined") {
return missingViewHandler();
// TODO: uncomment out next block
/*
Note: will jump back to previous level of view on 404
this is needed for regex route parsing
if (typeof previousView !== "undefined") {
_view = previousView.index;
} else {
return missingViewHandler();
}
*/
}

// TODO: route parsing
/*
var Route = require('route-parser');
// if a Hook.path is defined and there is a wildcard route present
if (typeof _view.presenter.route !== "undefined" && _view.presenter.route.length > 0
&& typeof req.params["0"] !== "undefined" && req.params["0"].length > 0) {
// attempt to match wildcard route ( recieved after /:owner/:hook/* ) against Hook.path
var route = new Route(req.hook.path);
var routeParams = route.match("/" + req.params["0"]);
// if no route match is found, 404 with a friendly error
if (routeParams === false) {
res.writeHead(404);
// TODO: make error customizable
res.end('Invalid path: ' + req.params["0"] + ' does not match ' + req.hook.path);
return;
}
// route matches, map route parameters to resource scope
for (var p in routeParams) {
req.resource.params[p] = routeParams[p];
}
}
*/

// Note: presenters are currently required for view middleware
// this could be changed easily by adding an additional method for default html rendering
if (typeof _view.present !== 'function') {
return missingViewHandler();
}

_view.present({
request: req,
response: res,
req: req,
res: res,
data: req.resource.params
}, function (err, rendered) {
if (err) {
if (err.code === "NO_PRESENTER_FOUND" && options.view.autoindex) {
return autoindex(_view, {
request: req,
response: res,
req: req,
res: res,
data: req.resource.params
}, function (err, html){
res.end(html);
Expand All @@ -68,11 +122,14 @@ module['exports'] = function (options) {
// If we hit a missing view and autoindex is enabled, and we actually have a view to autoindex
// Note: in most cases, it seems this is already taken care of...
// ...we could probably refactor and remove the following block
// console.log('missingViewHandler', req.url);
if (options.view.autoindex && typeof _view === "object") {
//var resp = Object.keys(_view.views);
return autoindex(_view, {
request: req,
response: res,
req: req,
res: res,
data: req.resource.params
}, function (err, html){
res.end(html);
Expand Down

0 comments on commit 004740b

Please sign in to comment.