This repository was archived by the owner on Jun 24, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
166 lines (120 loc) · 3.74 KB
/
Copy pathapp.js
File metadata and controls
166 lines (120 loc) · 3.74 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var connect = require('connect')();
var bodyParser = require('body-parser');
var favicon = require('serve-favicon');
var morgan = require('morgan');
var path = require('path');
var quip = require('quip');
var url = require('url');
var _ = require('underscore');
var AppClass = require('./lib/app');
var TemplateClass = require('./lib/template');
var Template = TemplateClass.extend(function() {
'use strict';
this.split_paths = function(_path) {
if (_.isArray(_path)) {
return _path;
}
var PATH = process.env.PATH.split(path.delimiter);
PATH.push(_path);
return PATH;
};
});
var App = AppClass.extend(function() {
'use strict';
this.base_dir_abs = function() {
return this._base_dir_abs || (this._base_dir_abs = ['tt']);
};
this.template_obj = function template_obj(args) {
if (this._template_obj) {
return this._template_obj;
}
if ('undefined' !== typeof Template) {
this._template_obj = new Template(args);
return this._template_obj;
}
return false;
};
this.path_info = function path_info(pathname) {
if ('undefined' !== typeof pathname) {
this._path_info = pathname.replace(/^\/$/, '');
}
return this._path_info;
};
this.script_name = function script_name() {
if (this._script_name) {
return this._script_name;
}
this._script_name = this.current_step() || __filename;
return this._script_name;
};
this.swap_template = function swap_template(step, file, swap) {
var template_obj = this.__template_obj(step);
var content;
if (template_obj) {
template_obj.process(file, swap, function(output) {
if (output) {
content = output;
}
return false;
});
}
return content;
};
this.print_out = function(step, out) {
var mimetype = this.run_hook('mimetype', step);
var charset = this.run_hook('charset', step);
var content_type = mimetype +
(charset && charset.match(/^[\w\-\.\:\+]+$/) ? '; charset=' + charset : '');
this.__res.mime(content_type, out);
this.__res.end();
};
});
// Print out the results of each method while in debug mode
if ('debug' === process.env.NODE_ENV) {
var meld = require('meld');
var debug_utils = require('./debug-utils');
// Allows wrapping each method call to see their arguments and returns
meld.after(App, debug_utils.get_methods, debug_utils.after_log);
meld.after(App.prototype, debug_utils.get_methods, debug_utils.after_log);
}
// Logging
connect.use(morgan('combined', {
skip: function(req, res) {
'use strict';
return 400 > res.statusCode;
}
}));
// Favicons support
connect.use(favicon(__dirname + '/favicon.ico'));
// Parse requests - application/x-www-form-urlencoded
connect.use(bodyParser.urlencoded({extended: false}));
// Parse requests - application/json
connect.use(bodyParser.json());
// Boot up main application
connect.use(function(req, res, next) {
'use strict';
var app = new App(
req,
quip(res)
);
var pathname = url.parse(req.url).pathname;
app.path_info(pathname);
var exception;
try {
app.navigate(req, res);
exception = false;
} catch(err) {
exception = err;
}
if (exception) {
next(exception);
}
});
// Error - 404
connect.use(require('./routes/errors/404'));
// Error - 500
connect.use(function(err, req, res, next) { // jshint ignore:line
'use strict';
quip(res).end(err.message);
});
exports = module.exports = connect;