-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodeDrive.js
202 lines (157 loc) · 4.38 KB
/
nodeDrive.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
"use strict";
const { parse } = require('querystring');
//ForeRunnerDB
let saveToDB = true;
let dbname = "FittsLaw";
let mouseCollectionName = "MousePositions";
let trialCollectionName = "Trials";
var db = null;
var mouseCollection = null;
var trialCollection = null;
// HTTP Portion
var http = require('http');
// Path module
var path = require('path');
// Using the filesystem module
var fs = require('fs');
var server = http.createServer(handleRequest);
server.listen(8080);
startDB();
console.log('Server started on port 8080');
function startDB() {
var ForerunnerDB = require("forerunnerdb");
var fdb = new ForerunnerDB();
db = fdb.db(dbname);
if(db !== null) {
db.persist.dataDir("./configData");
mouseCollection = db.collection(mouseCollectionName, {primaryKey: "TrialDate"});
trialCollection = db.collection(trialCollectionName, {primaryKey: "TrialDate"});
loadInformation();
console.log('DB started and Loaded');
} else {
throw "Could Not Create a DB";
}
}
function storeMouse(req, res) {
if (req.method === 'POST') {
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
let jsObj = JSON.parse(body);
console.log(jsObj);
mouseCollection.insert(jsObj);
res.end('ok');
});
}
}
function storeTrial(req, res) {
if (req.method === 'POST') {
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
let jsObj = JSON.parse(body);
console.log(jsObj);
trialCollection.insert(jsObj);
res.end('ok');
});
}
}
function getTrialData(req, res) {
console.log(trialCollection.find());
res.setHeader('Content-Type', 'application/json');
res.statusCode = 200;
// Send data
res.end(JSON.stringify(trialCollection.find()));
}
function getMouseData(req, res) {
console.log(mouseCollection.find());
res.setHeader('Content-Type', 'application/json');
res.statusCode = 200;
// Send data
res.end(JSON.stringify(mouseCollection.find()));
console.log(mouseCollection.find());
res.end('ok');
}
function saveTrialInformation(req, res) {
trialCollection.save(function (err) {
if (!err) {
console.log("Trial Information Saved Successfully");
}
});
mouseCollection.save(function (err) {
if (!err) {
console.log("Mouse Information Saved Successfully");
}
});
}
function loadInformation() {
trialCollection.load(function (err) {
if (!err) {
console.log("Trial Load Successful");
}
});
mouseCollection.load(function (err) {
if (!err) {
console.log("Mouse Load Successful");
}
});
}
function handleRequest(req, res) {
// What did we request?
var pathname = req.url;
console.log("Requested: " + pathname)
if(pathname === '/registerMouse/') {
storeMouse(req, res)
return;
}
if(pathname === '/registerTrial/') {
storeTrial(req, res)
return;
}
if(pathname === '/saveAllTrialData/') {
saveTrialInformation(req, res)
return;
}
if(pathname === '/getAllTrialInformation/') {
getTrialData(req, res)
return;
}
if(pathname === '/getAllMouseInformation/') {
getMouseData(req, res)
return;
}
// If blank let's ask for index.html
if (pathname === '/') {
pathname = '/home.html';
}
console.log("Converted: " + pathname)
// Ok what's our file extension
var ext = path.extname(pathname);
// Map extension to file type
var typeExt = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css'
};
// What is it? Default to plain text
var contentType = typeExt[ext] || 'text/plain';
console.log("Reading: " + __dirname + pathname)
// User file system module
fs.readFile(__dirname + pathname,
// Callback function for reading
function (err, data) {
// if there is an error
if (err) {
res.writeHead(500);
return res.end('Error loading ' + pathname);
}
// Otherwise, send the data, the contents of the file
res.writeHead(200,{ 'Content-Type': contentType });
res.end(data);
}
);
}