-
Notifications
You must be signed in to change notification settings - Fork 2
/
fileserver.js
89 lines (73 loc) · 1.82 KB
/
fileserver.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
var express = require('express');
var fs = require('fs');
var app = express();
// TODO make this async, this is for prototyping only
var users_file = JSON.parse(fs.readFileSync('public/users.json', 'utf8'));
var users_limit = 10;
var sendResponse = function(users, query, response) {
responseObject = {
"users": users,
"query": query
};
response.send(responseObject);
};
var findUser = function(query, user) {
query = query.toLowerCase();
if (typeof user === 'object') {
for (var i in user) {
if (user.hasOwnProperty(i)) {
if (Array.isArray(user[i])) {
for (var j = 0; j < user[i].length; j++) {
if (findUser(query, user[i][j])) {
return true;
}
}
}
else {
userSearch = user[i] + '';
if (userSearch.toLowerCase().indexOf(query) !== -1) {
return true;
}
}
}
}
} else {
user = user + '';
if (user.toLowerCase().indexOf(query) !== -1) {
return true;
}
}
};
var findUsers = function(query) {
var users = [];
for (var i in users_file) {
if (users.length >= users_limit) {
return users;
}
if (users_file.hasOwnProperty(i)) {
if (!query) {
users.push(users_file[i]);
}
else {
var userFound = findUser(query, users_file[i]);
if (userFound) {
users.push(users_file[i]);
}
}
}
}
return users;
};
var performSearch = function(query, response) {
var users = findUsers(query);
sendResponse(users, query, response);
};
app.get('/api/search/:query?', function(req, res){
var query = req.params.query;
performSearch(query, res);
});
app.configure(function(){
app.use(express.static(__dirname + '/public'));
});
var port = process.env.PORT || 3000;
app.listen(port);