-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpress.js
More file actions
149 lines (123 loc) · 3.7 KB
/
Copy pathexpress.js
File metadata and controls
149 lines (123 loc) · 3.7 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
var express = require('express');
var fs = require('fs');
var http = require('http');
var path = require('path');
var info = require('./requestinfo');
var bodyParser = require('body-parser');
var photoFolder = "./photos/";
var suffix = "group_";
// Main folder gets port 8080, otherwise gets 8081
var ismain = __dirname.indexOf("-MAIN") > -1;
var port = ismain ? 8080 : 8081;
var app = express();
var server = http.createServer(app);
if (!String.prototype.startsWith) {
String.prototype.startsWith = function(searchString, position) {
position = position || 0;
return this.indexOf(searchString, position) === position;
};
}
// print process.argv
process.argv.forEach(function (val, index, array) {
if (index < 2) {
return;
}
str = val.replace(/\s+/g, '');
if (str == "-h") {
console.log("./node express.js <-h> <port=num> <photoDir=loc> <suffix=str>");
process.exit();
}
else if (str.startsWith("port=")) {
port = str.split("=")[1];
}
else if (str.startsWith("photoDir=")) {
photoFolder = str.split("=")[1] + "/";
}
else if (str.startsWith("suffix=")) {
suffix = str.split("=")[1];
}
else {
console.log("Unknown argument: " + val);
}
});
var url = 'http://localhost:' + port + '/';
var randImages = [];
var logFiles = {};
console.log("Main service: " + ismain);
server.listen(port);
console.log(url);
// Date and time to string generator
function getDateTime() {
var date = new Date();
var hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
var min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;
var sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;
var year = date.getFullYear();
var month = date.getMonth() + 1;
month = (month < 10 ? "0" : "") + month;
var day = date.getDate();
day = (day < 10 ? "0" : "") + day;
return year + month + day + "T" + hour + min + sec;
}
app.use(bodyParser.urlencoded({extend: false}));
// Append to file wrapper
function fileWriter(fileName, text) {
fs.appendFileSync(fileName, text);
}
// Logger of action vs. submission
app.post('/data', function(req, res) {
var user = req.body.user;
var logF = req.body.log;
var filePath = "triplets/triplets_page_" + logF + ".log";
var isSubmit = req.body.isSubmit;
if (isSubmit == "true") {
var comment = req.body.comm;
fileWriter(filePath, "Submitted by " + user + ":\t" + comment + "\n*******************************\n\n");
} else {
var mark = req.body.mark;
var src = req.body.src;
var msg = getDateTime() + "\tImage by " + user + ":\t" + src + "\t" + mark;
fileWriter(filePath, msg + "\n");
}
res.end();
});
// New use gets new stack of images and data
app.get('/newUser', function(req, res) {
user = req.query.user;
var time = getDateTime();
var countData = info.countSources(photoFolder, suffix);
response = user + "_" + time;
res.send({user: response, srcs: countData.srcs, grps: countData.grps});
});
// Main url - just show index.html
app.get('/', function (req, res) {
info.resetAll(photoFolder, suffix);
res.sendFile(__dirname + path.normalize('/index.html'));
});
// GET of new images
app.get('/newImage', function (req, res) {
randImages = info.getRandImages(photoFolder, req.query.num, req.query.page);
res.send(randImages);
});
// Misc gallery
app.get('/gallery', function (req, res) {
res.writeHead(302, {
'Location': '/PhotoFloat/web/index.html'
});
res.end();
});
// Resets all data (in case groups change)
app.get('/reset', function(req, res){
info.resetAll(photoFolder, suffix);
res.writeHead(302, {
'Location': '/'
});
res.end();
});
// Any other case - just try accessing that path
app.get('/*', function (req, res) {
res.sendFile(__dirname + path.normalize(info.getPath(req)));
});