-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiController.js
More file actions
53 lines (43 loc) · 1.27 KB
/
apiController.js
File metadata and controls
53 lines (43 loc) · 1.27 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
const mongo_utils = require('../utils/mongo_utils');
//fetch/events [get]
exports.fetchEvent = async(req, res)=>{
try{
res.contentType = 'application/json';
const getName = req.query.name;
const getType = req.query.type;
const getDate = req.query.date;
const query = {};
if(getName) {
query.name = { $regex: getName, $options: 'i' };
}
if(getType) {
query.type = getType;
}
if(getDate) {
query.date = getDate;
}
const db = mongo_utils.get_client().db();
const collection = db.collection('events');
const result = await collection.find(query).toArray();
res.send({
message: result,
})
} catch(e) {
console.log(e);
res.status(400).send({ msg: 'Internal Server Error' });
return;
}
}
///fetch/images/:img_name
exports.fetchImage = async(req, res)=>{
try{
const img_name = req.params.img_name;
const bucket = mongo_utils.get_bucket();
const downloadStream = bucket.openDownloadStreamByName(img_name);
downloadStream.pipe(res);
}catch(e){
console.log(e);
res.status(404).send({msg:'error'});
return;
}
}