-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
89 lines (75 loc) · 2.26 KB
/
index.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
const express = require('express');
const bodyParser = require('body-parser');
const pathToRegexp = require('path-to-regexp');
const app = express();
const MongoClient = require('mongodb').MongoClient;
const mongoUrl = 'mongodb://mongo:27017';
let mongodb = null;
const prom = require('prom-client');
const prom_gc = require('prometheus-gc-stats');
prom_gc();
process.env.MONGO_URL_FILE
app.use(bodyParser.json());
let counter =0;
//app.use(cors.json());
app.get('/blabs', (req, res) => {
/*const thing = items.filter(function (item){
let x = req.query.createdSince;
if (x==undefined) x=0;
return item.postTime>=x;
});
*/
let x = req.query.createdSince;
if (x==undefined) x=0;
console.log(`createdSince=${x}`);
mongoDb.collection('blabs')
.find({
postTime: {$gte: parseInt(x)}
}).toArray()
.then(function(item) {
res.status(200).send(item);
});
//res.status(200).send(thing);
});
app.get('/metrics', function(req, res) {
res.end(prom.register.metrics());
});
app.post('/blabs', (req, res) => {
const newItem = {
author:{
name : req.body.author.name,
email: req.body.author.email,
},
message : req.body.message,
postTime : Math.floor(Date.now()/1000),
id : counter++,
}
//items.push(newItem);
//res.status(201).send(newItem);
mongoDb.collection('blabs')
.insertOne(newItem)
.then(function(response) {
res.status(201).send(newItem);
});
});
app.delete('/blabs/:blabid', (req, res) => {
//const regexp = pathToRegexp('/blabs/:blabid', keys);
//const id = req.params;
//console.log(`your server is running on port ${id}`);
/*items = items.filter(function (i){
return i.id!==parseInt(id.blabid);
});
*/
//if (req.params.blabid===2) mongoDb.collection('blabs').remove( { } );
mongoDb.collection('blabs').deleteOne({ id: {$eq: parseInt(req.params.blabid)}});
res.status(200).send(`deleted blab has id ${req.params.blabid}`);
});
MongoClient.connect(mongoUrl, function(err, client) {
if (err)
throw err;
console.log("Connected successfully to server");
mongoDb = client.db();
app.listen(3000, () => {
console.log('Listening on port 3000');
});
});