-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
108 lines (91 loc) · 2.7 KB
/
app.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
var express = require('express')
var bodyParser = require('body-parser')
var path = require('path');
var Datastore = require('nedb'),
db = new Datastore({
// filename: 'database/feedbackdatabase.db',
autoload: true
});
var app = express();
app.use(bodyParser.json());
db.remove({}, {
multi: true
}, function(err, numRemoved) {
console.log(numRemoved);
});
//luodaan pari kysymystä
var kysymys1 = {
kysymys: 'Miten menee?',
date: new Date(),
vastauslista: []
};
var kysymys2 = {
kysymys: 'Kuinka kulkee?',
date: new Date(),
vastauslista: []
};
db.insert(kysymys1, function(err, newDoc) { // Callback is optional
var vastaus1 = {
vastaus: 'apinaaaa'
};
newDoc.vastauslista.push(vastaus1);
db.update({ _id: newDoc._id }, newDoc , {}, function(err, numReplaced) {
console.log("korvattuja: ",numReplaced);
// The doc #3 has been replaced by { _id: 'id3', planet: 'Pluton' }
// Note that the _id is kept unchanged, and the document has been replaced
// (the 'system' and inhabited fields are not here anymore)
});
});
db.insert(kysymys2, function(err, newDoc) { // Callback is optional
var vastaus1 = {
vastaus: 'apinaaaa'
};
newDoc.vastauslista.push(vastaus1);
db.update({ _id: newDoc._id }, newDoc , {}, function(err, numReplaced) {
console.log("korvattuja: ",numReplaced);
// The doc #3 has been replaced by { _id: 'id3', planet: 'Pluton' }
// Note that the _id is kept unchanged, and the document has been replaced
// (the 'system' and inhabited fields are not here anymore)
});
});
app.get('/get.json', function(req, res) {
db.find({}).sort({ date: 1 }).exec(function(err, docs) {
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(docs));
})
})
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/public/angular_form.html'));
})
app.post('/tallenna', function(req, res) {
for(var i in req.body ){
db.update({ _id: i }, { $push: { vastauslista: { vastaus: req.body[i] } }}, {}, function () {
});
};
res.sendStatus(200);
})
app.post('/tallennakysymys', function(req, res) {
var uusikysymys = {
kysymys: req.body.kysymys,
date: new Date(),
vastauslista: []
};
db.insert(uusikysymys);
db.find({}, function(err, docs) {
console.log(docs);
res.sendStatus(200);
});
})
app.post('/poistakysymys', function(req, res) {
db.remove({ _id: req.body.kysymys_id }, {}, function (err, numRemoved) {
if (err) {
console.log(err);
}else{
console.log("poistettu ", numRemoved);
}});
db.find({}, function(err, docs) {
console.log(docs);
res.sendStatus(200);
});
})
app.listen(8080);