-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
166 lines (130 loc) · 3.97 KB
/
app.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
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
require('coffee-script');
var express = require('express'),
path = require('path'),
normalize = require('normalize'),
Session = require('connect-mongodb'),
Backbone = require("backbone"),
_ = require("underscore"),
$ = require("cheerio"),
fs = require('fs'),
Chaplin = require('./src/chaplin'),
config = require('./config'),
utils = require('./lib/utils'),
app = express(),
baseDir = config.baseDir = path.normalize(__dirname),
views = baseDir + "/views",
webroot = baseDir + "/public",
api,
host,
port,
stylus,
webroot,
_ref,
routes = require('./shared/routes'),
Application = require('./shared/application'),
client_app,
LayoutManager;
LayoutManager = Chaplin.MgrView;
// Configure LayoutManager with some very useful defaults for Node.js
// environments. This allows the end user to simply consume instead of
// fighting with the desirable configuration.
LayoutManager.configure({
prefix: "shared/templates/",
// Sensible default for Node.js is to load templates from the filesystem.
// This is similar to how we default to script tags in browser-land.
fetchTemplate: function (template) {
// Automatically add the `.html` extension.
template = template + ".html";
// Put this fetch into `async` mode to work better in the Node environment.
var done = this.async();
// By default read in the file from the filesystem relative to the code
// being executed.
fs.readFile(template, function (err, contents) {
// Ensure the contents are a String.
contents = String(contents);
// Any errors should be reported.
if (err) {
console.error("Unable to load file " + template + " : " + err);
return done(null);
}
// Pass the template contents back up.
done(_.template(contents));
});
}
});
app.enable('trust proxy');
app.set('views', views);
app.set('view engine', 'ejs');
app.set('view options', {
layout: false
});
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express["static"](webroot));
app.use('/upload', express["static"](baseDir + "/upload"));
app.use(express.bodyParser({
uploadDir: baseDir + '/tmp'
}));
app.use(express.cookieParser());
/*app.use(express.session({
key: 'session_id',
store: new Session({
url: config.db,
maxAge: 300000
}),
secret: config.httpd.session_key || "balabala"
}));*/
app.use(function (req, resp, next) {
resp.header('Access-Control-Allow-Origin', config.allowedDomains || '*');
resp.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
resp.header('Access-Control-Allow-Headers', 'Content-Type');
resp.header('Cache-Control', 'no-cache');
return next();
});
//auth(app);
/**
* handle the backbone routers
*/
app.use(function (req, resp, next) {
var location = new utils.Location(req, resp),
setSubVwKeepFn;
setSubVwKeepFn = function(vw) {
var subVws = vw.getViews();
subVws.each(function (view) {
/**
* set the view's state to keep
*/
view.keep = true;
});
};
client_app = new Application({
location: location,
// events: events,
routes: routes,
controllerSuffix: '-controller',
onViewRendered: function (controller, params, route, options) {
var vw = controller.view,
manager = vw.__manager__,
currentVw = vw,
html;
setSubVwKeepFn(currentVw);
while (manager.parent && manager.parent.__manager__) {
currentVw = manager.parent;
manager = currentVw && currentVw.__manager__;
if(currentVw) {
setSubVwKeepFn(currentVw);
}
}
currentVw.render().promise().then(function () {
html = $.html(currentVw.$el);
resp.render('layout', { body: html })
});
}
});
});
//router(app);
//api = app.use('/api', require('./lib/api'));
host = config.httpd.bind || "127.0.0.1";
port = process.env.PORT || config.httpd.port || 3000;
console.info("Listening on port " + host + ":" + port);
app.listen(port, host);