-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.js
123 lines (101 loc) · 2.96 KB
/
web.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
/**
* Cloudspokes challenge #1840 - back-end server
*
* web.js - Web server with routing and RESTful API
*
* Features:
* - express for the main framework
* - mongodb for database
* - pusher for server sent events
* - opencv (special build for heroku, compiled with vulcan), non-blocking,
* scalable, forked child-processes
*
* @author [email protected]
* @version
*/
// heroku environment || local
var port = process.env.PORT || 3000;
/* Module dependencies. */
var express = require('express'),
path = require('path'),
routes = require('./routes'),
sessionManager = require('./session.js'),
pusher = require('./pusher.js'),
pusher_server = require('./pusher_server.js'),
cv = require('opencv-node'),
// module of object detection (faces && eyes)
Detector = require('./detector'),
sys = require('sys'),
fs = require('fs');
var Mat = cv.Mat;
var app = module.exports = express();
/* Application constants (setup) */
app.set('port', port);
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.set('uploads', __dirname + '/public/data/upload');
app.set('view cache', false);
//app.set('source', __dirname + '/source');
/**
* mustKillCache middleware
*
* needed, to not let the client cache REST calls
*/
var mustKillCache = function (req, res, next) {
if (req.url === '/') {
res.header("Cache-Control", "no-cache, no-store, must-revalidate");
res.header("Pragma", "no-cache");
res.header("Expires", 0);
}
next();
}
/* MiddleWare stack */
app.configure(function(){
app.use(express.logger('dev'));
app.use(express.bodyParser({
keepExtensions: true,
uploadDir: app.get('uploads') }));
app.use(express.limit('5mb'));
app.use(express.methodOverride());
app.use(express.cookieParser('supasecret')); // not so...
app.use(express.session({
cookie: { maxAge: 60000 * 20, // 20 minutes
secure: false, // need https for this
httpOnly: true,
expires: false }
}));
/* No Cache Middleware for the API */
app.use(mustKillCache);
app.use(express.static(path.join(__dirname, '/public')));
app.use(app.router);
app.use(function(err, req, res, next){
// handle registering session errors...
if(res.statusCode === 307){
res.send('Redirecting...');
}else{
next(err);
}
});
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
/* Request routing */
// routing of the main pages to index.html - public access
app.get('/', sessionManager.preroute, routes.index);
// File upload:
app.post('/upload', sessionManager.fileupload);
// Opencv calculus trigger
app.post('/calculate', sessionManager.opencv );
/* Pusher messages */
// authorize endpoint
app.post('/auth', sessionManager.auth);
app.post('/handshake', sessionManager.handshake);
/* Startup application */
if (!module.parent) {
app.listen(app.get('port'));
console.log('Express started on port '+ app.get('port'));
}