-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.js
63 lines (53 loc) · 1.89 KB
/
api.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
var express = require('express');
var escutils = require('./escutils');
var firebase = require('firebase');
var SearchQueue = require('./lib/SearchQueue');
var DynamicPathMonitor = require('./lib/DynamicPathMonitor');
var PathMonitor = require('./lib/PathMonitor');
var router = express.Router();
//----------------------------------------------------------------------------------------------
// middleware that is specific to this router
router.use(function timeLog(req, res, next) {
console.log('Time: ', Date.now(), ',Request: ', req.baseUrl);
req.requestTime = Date.now();
next();
});
// define the home page route
router.get('/', function(req, res) {
var responseText = 'You are at: ' + req.baseUrl + '<br>';
responseText += '<small>Requested at: ' + req.requestTime + '</small>';
res.send(responseText);
});
//----------------------------Search users by email --------------------------------------------------
router.get('/index/users/email/:email', function(req, res) {
var searchedEmail = req.params.email;
escutils.elasticSearchClient.search({
q: `${searchedEmail}`, //query user's email
index: "indeex",
type: "user"
}, function(err, data) {
if (err) {
return res.status(400).json({
error: err
});
}
res.json(data);
});
});
//----------------------------Search users by username -------------------------------------------------
router.get('/index/users/username/:username', function(req, res) {
var username = req.params.username;
escutils.elasticSearchClient.search({
q: `*${username}*`, //use ** chars to complete search
index: "index",
type: "user"
}, function(err, data) {
if (err) {
return res.status(400).json({
error: err
});
}
res.json(data);
});
});
module.exports = router;