-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
126 lines (109 loc) · 3.67 KB
/
server.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
/* global console */
var path = require('path');
var express = require('express');
var helmet = require('helmet');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var Moonboots = require('moonboots-express');
var compress = require('compression');
var config = require('getconfig');
var semiStatic = require('semi-static');
var serveStatic = require('serve-static');
var stylizer = require('stylizer');
var templatizer = require('templatizer');
var app = express();
// a little helper for fixing paths for various environments
var fixPath = function (pathString) {
return path.resolve(path.normalize(pathString));
};
// -----------------
// Configure express
// -----------------
app.use(compress());
app.use(serveStatic(fixPath('public')));
// we only want to expose tests in dev
if (config.isDev) {
app.use(serveStatic(fixPath('test/assets')));
app.use(serveStatic(fixPath('test/spacemonkey')));
}
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// in order to test this with spacemonkey we need frames
if (!config.isDev) {
app.use(helmet.xframe());
}
app.use(helmet.xssFilter());
app.use(helmet.nosniff());
app.set('view engine', 'jade');
// -----------------
// Set up our little demo API
// -----------------
var api = require('./fakeApi');
app.get('/api/people', api.list);
app.get('/api/people/:id', api.get);
app.delete('/api/people/:id', api.delete);
app.put('/api/people/:id', api.update);
app.post('/api/people', api.add);
// -----------------
// Enable the functional test site in development
// -----------------
if (config.isDev) {
app.get('/test*', semiStatic({
folderPath: fixPath('test'),
root: '/test'
}));
}
// -----------------
// Set our client config cookie
// -----------------
app.use(function (req, res, next) {
res.cookie('config', JSON.stringify(config.client));
next();
});
// ---------------------------------------------------
// Configure Moonboots to serve our client application
// ---------------------------------------------------
new Moonboots({
moonboots: {
jsFileName: 'colloqi-client',
cssFileName: 'colloqi-client',
main: fixPath('client/app.js'),
developmentMode: config.isDev,
libraries: [
],
stylesheets: [
fixPath('stylesheets/bootstrap.css'),
fixPath('stylesheets/app.css')
],
browserify: {
debug: config.isDev
},
beforeBuildJS: function () {
// This re-builds our template files from jade each time the app's main
// js file is requested. Which means you can seamlessly change jade and
// refresh in your browser to get new templates.
if (config.isDev) {
templatizer(fixPath('templates'), fixPath('client/templates.js'));
}
},
beforeBuildCSS: function (done) {
// This re-builds css from stylus each time the app's main
// css file is requested. Which means you can seamlessly change stylus files
// and see new styles on refresh.
if (config.isDev) {
stylizer({
infile: fixPath('stylesheets/app.styl'),
outfile: fixPath('stylesheets/app.css'),
development: true
}, done);
} else {
done();
}
}
},
server: app
});
// listen for incoming http requests on the port as specified in our config
app.listen(config.http.port);
console.log('colloqi-client is running at: http://localhost:' + config.http.port + ' Yep. That\'s pretty awesome.');