-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
90 lines (75 loc) · 3.52 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
var fs = require('fs'),
path = require('path'),
cwd = process.cwd(),
DEFAULT_CONFIG = require('./config');
/**
* Register express-engine for a given Express Application.
*
* @param {Object} app Express Application.
* @param {Object} [userConfig] User supplied configuration, will be merged into (not override) default configuration.
*/
module.exports = function(app, userConfig) {
var config = {},
key;
for (key in DEFAULT_CONFIG) {
config[key] = DEFAULT_CONFIG[key];
}
if (userConfig && typeof userConfig === 'object') {
for (key in userConfig) {
config[key] = userConfig[key];
}
}
(function traverse(directory) {
fs.readdirSync(directory).forEach(function(file) {
var endPoint = directory + '/' + file;
if (fs.lstatSync(endPoint).isDirectory()) {
traverse(endPoint);
} else {
var fileName = directory.replace(app.get('views'), '') + '/' + path.basename(file, path.extname(file)),
baseFileName = fileName.substring(1, fileName.length),
urlMappings = config.url_mappings[baseFileName],
urls = (typeof urlMappings === 'string' || urlMappings instanceof Array) ? urlMappings : fileName,
controller,
javascriptFile,
stylesheetFile;
if (!(urls instanceof Array)) {
urls = [urls];
}
if (fs.existsSync(path.normalize(cwd + '/' + config.controllers_location) + fileName + '.js')) {
controller = require(path.normalize(cwd + '/' + config.controllers_location) + fileName + '.js');
}
if (fs.existsSync(path.normalize(cwd + '/' + config.public_location + '/' + config.javascripts_location) + fileName + '.js')) {
javascriptFile = path.normalize('/' + config.javascripts_location) + fileName + '.js';
}
if (fs.existsSync(path.normalize(cwd + '/' + config.public_location + '/' + config.stylesheets_location) + fileName + '.css')) {
stylesheetFile = path.normalize('/' + config.stylesheets_location) + fileName + '.css';
}
urls.forEach(function(url) {
if (url[0] !== '/') {
url = '/' + url;
}
app.get(url, function(req, res, next) {
res.locals[config.javascripts_location_property] = javascriptFile;
res.locals[config.stylesheets_location_property] = stylesheetFile;
res.locals[config.view_location_property] = baseFileName;
next();
});
if (controller) {
app.get(url, function(req, res, next) {
controller(req, res, function(err, result) {
if (!res.headersSent) {
if (err) {
next(err);
} else {
res.locals[config.controller_result_property] = result;
next();
}
}
});
});
}
});
}
});
}(app.get('views')));
};