-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
122 lines (104 loc) · 3.55 KB
/
server.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
var express = require('express'),
app = express(),
router = express.Router(),
bodyParser = require('body-parser'),
morgan = require('morgan'),
mongoose = require('mongoose'),
Rating = require('./app/models/rating'),
port = Number(process.env.PORT || 8000),
fs = require('fs');
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
mongoose.connect('mongodb://dimaSavchenko:[email protected]:47468/heroku_1jr2bppx', function(error){
console.log('connect to mongo');
if(error) {
console.log(error);
}
});
router.use(function(req, res, next) {
console.log('use api');
next();
});
router.get('/', function(req, res) {
res.json({message: 'It is get request'});
});
router.route('/rating')
.post(function(req, res) {
req.body.forEach(function(ratingReq){
var query = {
word: ratingReq.word,
partOfSpeech: ratingReq.partOfSpeech
};
Rating.findOne(query, function(error, doc){
if(doc == null) {
var newRating = new Rating();
newRating.word = ratingReq.word;
newRating.dicNumber = ratingReq.dicNumber;
newRating.partOfSpeech = ratingReq.partOfSpeech;
newRating.rating = ratingReq.rating;
newRating.number = 1;
newRating.save(function(error) {
if (error) {
res.send(500, error);
}
});
}else {
var updateRating = doc;
updateRating.rating = (updateRating.rating * updateRating.number + Number(ratingReq.rating))/ (updateRating.number + 1);
updateRating.number += 1;
updateRating.save(function(error) {
if (error) {
res.send(500, error);
}
});
}
});
});
res.json({message: 'ratings updated'});
})
.get(function(req, res) {
Rating.find(function(err, doc) {
if (err) {
res.send(500, err);
}
res.json(doc);
});
})
.delete(function (req, res) {
Rating.remove({} , function(err) {
if (err) {
res.send(500, err);
}
res.json({message: 'deleted all document'});
});
});
router.route('/ratingToCSV')
.get(function (req, res) {
Rating.find({number: {$gt: 100}}, {'_id': 0, 'number': 0, '__v': 0}, function (err, doc) {
if (err) {
res.send(500, err);
}
var docToCSV = JSON.stringify(doc).replace(/},{/g, ';');
docToCSV = docToCSV.replace(/\[{/, '')
.replace(/}\]/, ';')
.replace(/"rating":/g, '')
.replace(/"partOfSpeech":/g, '')
.replace(/"dicNumber":/g, '')
.replace(/"word":/g, '')
.replace(/;/g, ';\n')
.replace(/,/g, ';')
.replace(/\./g, ',');
docToCSV = 'rating;partOfSpeech;dicNumber;word;\n' + docToCSV;
fs.writeFile('rating.csv', docToCSV, () => {
res.download(__dirname + '/rating.csv');
});
});
});
app.use('/', router);
app.listen(port, function(error) {
console.log('connect to app');
if(error) {
console.log(error);
}
});