-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
164 lines (143 loc) · 3.97 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
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
/**
* http://usejsdoc.org/
*/
var express = require('express');
var bodyParser = require('body-parser');
var etag = require('etag');
var fs = require('fs');
var url = require('url');
var app = express();
app.use(require('etagify')());
function returnRequestedFile(response, fileName) {
response.sendFile(fileName, {root: __dirname}, function(err) {
if (err) {
console.log(err);
response.status(err.status).end();
} else {
console.log("Sent: ", fileName);
}
});
}
//function createETag(fileName) {
// var eTag;
// fs.readFile(fileName, function(err, buf) {
// eTag = crypto.createHash('md5').update(buf).digest('hex');
// });
// return eTag;
//}
//app.put('/data.csv', function(res, req) {
//
//});
app.use(bodyParser.text({type: "text/csv"}));
app.get('/pages/img/*.png',function(request, response) {
response.append('cache-control', 'max-age=1000000000');
var fileName = request.path;
console.log(fileName);
fileName = fileName.replace('pages/', "");
response.etagify();
var reqTag = request.get("etag");
// console.log("Type of ETag: " + (typeof reqTag));
if (reqTag !== undefined) {
if (reqTag === response.get("etag")) {
response.status(304);
console.log('Not Modified');
response.send("Not Modified");
} else {
returnRequestedFile(response, fileName);
}
} else {
returnRequestedFile(response, fileName);
}
});
app.get('*/node_modules/*.min.*', function(request, response) {
var path = request.path;
path = path.replace('/pages/', "");
returnRequestedFile(response, path);
});
app.get('*/pages/js/*', function(request, response) {
var path = request.path;
path = path.replace(/.*\/pages\//, "/public/");
console.log("Requested js: %s", path);
returnRequestedFile(response, path);
});
app.get('*/pages/css/*', function(request, response) {
var path = request.path;
path = path.replace(/.*\/pages\//, "/public/");
console.log("Requested CSS: %s", path);
returnRequestedFile(response, path);
});
app.get('*/pages/scripts/*.min.*', function(request, response) {
var dict = {
'angular.min.': 'node_modules/angular/',
'jquery.min.': 'node_modules/jquery/dist/',
'angular-sanitize.min.': 'node_modules/angular-sanitize/',
'angular-cookies.min.': 'node_modules/angular-cookies/',
'tooltip.js': '/lib/bootstrap-3.3.6/js/'
};
var path = request.path;
path = path.replace('*/pages/', "");
var name = path;
name = name.replace(/(.*\/)+((\w+.)+\w+)/, "$2");
console.log('name: %s', name);
for (var key in dict) {
if (name.match(key)) {
path = dict[key] + name;
}
}
returnRequestedFile(response, path);
});
app.get('*/jquery-ui*')
app.get('css/*.css', function(request, response) {
var path = request.path;
returnRequestedFile(response, path);
});
//app.get('*/build/img/*.png', function(request, response) {
// var url = request.path;
// url = url.replace('build/', '');
// console.log('URL', url);
// returnRequestedFile(response, url);
//});
app.put('*.csv', function(request, respond) {
var body = '';
filePath = "data.csv";
// count = 0;
request.on('data', function(data) {
body += data;
// count++;
});
// console.log(count);
// console.log("");
// console.log("");
// console.log(body);
request.on('end', function (){
fs.writeFile(filePath, decodeURI(body), function() {
respond.end();
console.log(decodeURI(body));
});
});
});
//app.listen(8080);
//
//
//send = function(req, res) {
// req.send(res.body);
//};
//
//
//app.put('/data.csv', function (req, res) {
// console.log(req.`body)
// console.log(req)
//}, send);
//
app.get('/*.csv', function(req, res) {
res.download("data.csv", decodeURI(req.path));
});
app.use('/', express.static('./'));
var serverRegular = app.listen(3000, '0.0.0.0', function() {
var addr = serverRegular.address();
console.log("Listening @ http://%s:%d", addr.address, addr.port);
});
var serverDebug = app.listen(5555, 'localhost', function() {
var addr = serverDebug.address();
console.log("Debug server listening @ http://%s:%d", addr.address, addr.port);
});