Skip to content

Commit 97dd907

Browse files
committed
feat(serveStatic): allow mapping single or multiple routes to a directory
1 parent cc2f4d9 commit 97dd907

File tree

1 file changed

+96
-23
lines changed

1 file changed

+96
-23
lines changed

lib/server/utils.js

Lines changed: 96 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var Map = require("immutable").Map;
1010
var snippet = require("./../snippet").utils;
1111
var _ = require("./../../lodash.custom");
1212
var serveStatic = require("serve-static");
13+
var logger = require("../logger");
1314

1415
var utils = {
1516
/**
@@ -82,30 +83,23 @@ var utils = {
8283
})
8384
}
8485

85-
if (options.get('serveStatic')) {
86-
options
87-
.get("serveStatic")
88-
.forEach(function (dir, i) {
89-
if (Immutable.Map.isMap(dir)) {
90-
var ssOptions = (function () {
91-
if (dir.get('options')) {
92-
return dir.get('options').toJS();
93-
}
94-
return {}
95-
})();
96-
defaultMiddlewares.push({
97-
id: "Serve static " + i,
98-
route: dir.get("route"),
99-
handle: serveStatic(dir.get("dir"), ssOptions)
100-
});
101-
} else if (_.isString(dir)) {
102-
defaultMiddlewares.push({
103-
id: "Serve static " + i,
104-
route: "",
105-
handle: serveStatic(dir, options.get('serveStaticOptions').toJS())
106-
});
107-
}
86+
if (options.get("serveStatic")) {
87+
88+
var ssMiddlewares = utils.getServeStaticMiddlewares(options.get("serveStatic"), options.get("serveStaticOptions", Immutable.Map({})).toJS());
89+
var withErrors = ssMiddlewares.filter(function(x) { return x.errors.length > 0 });
90+
var withoutErrors = ssMiddlewares.filter(function(x) { return x.errors.length === 0 });
91+
92+
if (withErrors.size) {
93+
withErrors.forEach(function (item) {
94+
logger.logger.error("{red:Warning!} %s", item.errors[0].data.message);
95+
});
96+
}
97+
98+
if (withoutErrors.size) {
99+
withoutErrors.forEach(function (item) {
100+
defaultMiddlewares.push.apply(defaultMiddlewares, item.items);
108101
});
102+
}
109103
}
110104

111105
/**
@@ -175,6 +169,85 @@ var utils = {
175169
res.setHeader("Access-Control-Allow-Credentials", true);
176170
next();
177171
}
172+
},
173+
getServeStaticMiddlewares: function (ssOption, serveStaticOptions) {
174+
175+
return ssOption.map(function (dir, i) {
176+
177+
if (Immutable.Map.isMap(dir)) {
178+
179+
var ssOptions = (function () {
180+
if (dir.get("options")) {
181+
return dir.get("options").toJS();
182+
}
183+
return {}
184+
})();
185+
186+
var route = dir.get("route");
187+
var _dir = dir.get("dir");
188+
189+
if (!isValidOption(route) || !isValidOption(_dir)) {
190+
return {
191+
items: [],
192+
errors: [{
193+
type: "Invalid Object",
194+
data: {
195+
message: "Serve Static requires both 'route' and 'dir' options when using an Object"
196+
}
197+
}]
198+
}
199+
}
200+
var ssItems = (function () {
201+
if (_.isString(route)) {
202+
return [{
203+
id: "Serve static " + i,
204+
route: getRoute(route),
205+
handle: serveStatic(_dir, ssOptions)
206+
}]
207+
}
208+
return route.map(function (item, j) {
209+
return {
210+
id: "Serve static " + i + "." + j,
211+
route: getRoute(item),
212+
handle: serveStatic(_dir, ssOptions)
213+
}
214+
}).toJS()
215+
})();
216+
return {
217+
items: ssItems,
218+
errors: []
219+
};
220+
}
221+
222+
if (_.isString(dir)) {
223+
return {
224+
items: [
225+
{
226+
id: "Serve static " + i,
227+
route: "",
228+
handle: serveStatic(dir, serveStaticOptions)
229+
}
230+
],
231+
errors: []
232+
}
233+
}
234+
235+
return {
236+
items: [],
237+
errors: [{
238+
type: "Invalid Type",
239+
data: {
240+
message: "Only strings and Objects (with route+dir) are supported for the ServeStatic option"
241+
}
242+
}]
243+
}
244+
});
245+
function isValidOption (x) {
246+
return _.isString(x) || (_.isArray(x) && x.length > 0) || (Immutable.List.isList(x) && x.size > 0);
247+
}
248+
function getRoute (x) {
249+
return x[0] === "/" ? x : "/" + x;
250+
}
178251
}
179252
};
180253

0 commit comments

Comments
 (0)