-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
101 lines (82 loc) · 2.92 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
90
91
92
93
94
95
96
97
98
99
100
101
const fs = require('fs');
const express = require('express');
const bodyParser = require('body-parser');
const morgan = require('morgan');
const app = express()
.enable('trust proxy')
.use(bodyParser.json())
.use(bodyParser.urlencoded({ extended: true }))
.use(morgan('combined'));
const MongoClient = require('mongodb').MongoClient;
const config = require('./config');
function find(collection, criteria) {
return new Promise((resolve, reject) => {
collection.find(criteria).toArray((err, docs) => {
if (err) return reject(err);
resolve(docs);
});
});
}
module.exports = function(opts = {}) {
Object.assign(config, opts);
const mongoUrl = config.mongo;
const urlPrefix = config.pathPrefix;
const dbConnection = new Promise((resolve, reject) => {
MongoClient.connect(mongoUrl, function(err, client) {
if (err) return reject(err);
console.log('Connected successfully to mongo');
resolve(client.db('feedbacks'));
});
});
return dbConnection.then(db => {
const collection = db.collection('docs');
app
.get(`/${urlPrefix}`, (req, res) => {
console.log('get ', req.url, req.query);
const doc = encodeURIComponent(req.query.doc);
find(collection, { doc }).then(docData => {
const total = docData.reduce((acc, feedback) => {
return acc + +feedback.rating;
}, 0);
res.send({
total,
votes: docData.length
});
});
})
.post(`/${urlPrefix}`, (req, res) => {
console.log('post ', req.url, req.body);
// NOTE: for some reason the data is in req.query when posted from localhost
const data = Object.assign({
ip: req.headers['x-forwarded-for'] || req.connection.remoteAddress,
date: new Date(),
}, req.query, req.body);
collection.insert(data, (err, result) => {
if (err) return res.status(500).send('DB error :(');
res.send('ok');
});
});
return app;
});
};
if (!module.parent) {
const portOrSocket = config.portOrSocket;
if (isNaN(portOrSocket)) {
if (fs.existsSync(portOrSocket)) {
try {
fs.unlinkSync(portOrSocket);
} catch (e) { }
}
}
module.exports()
.then(app => {
app.listen(portOrSocket, () => {
console.log('Server is listening on', portOrSocket);
});
isNaN(portOrSocket) && fs.chmod(portOrSocket, '0777');
})
.catch(e => {
console.error('Error ---> ', e.stack || e);
process.exit(1);
});
}