-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodeServer.js
120 lines (104 loc) · 2.82 KB
/
nodeServer.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
// server.js
// DEPENDENCIES AND SETUP
// ===============================================
var express = require('express'),
app = express(),
port = Number(process.env.PORT || 8080);
var dir={
http:'page'
}
app.use(express.static(dir.http ));
var bodyParser = require('body-parser'); // Middleware to read POST data
// Set up body-parser.
// To parse JSON:
app.use(bodyParser.json());
// To parse form data:
app.use(bodyParser.urlencoded({
extended: true
}));
// DATABASE
// ===============================================
// Setup the database.
var Datastore = require('nedb');
var db = new Datastore({
filename: 'goals.db', // provide a path to the database file
autoload: true, // automatically load the database
timestampData: true // automatically add and manage the fields createdAt and updatedAt
});
// Let us check that we can save to the database.
// Define a goal.
var goal = {
description: 'Do 10 minutes meditation every day',
};
// Save this goal to the database.
db.find({}).sort({
updatedAt: -1
}).exec(function(err, goals) {
if (err) res.send(err)
else if(!goals){
db.insert(goal, function(err, newGoal){
if (err) console.log(err);
console.log(newGoal);
})
}
else{
console.log('ja existe meditacao');
}
});
// ROUTES
// ===============================================
// Define the home page route.
app.get('/', function(req, res) {
res.sendFile('page/index.html');
//res.send('Our first route is working. :)');
});
// GET all goals.
// (Accessed at GET http://localhost:8080/goals)
app.get('/goals', function(req, res) {
db.find({}).sort({
updatedAt: -1
}).exec(function(err, goals) {
if (err) res.send(err);
res.json(goals);
});
});
// POST a new goal.
// (Accessed at POST http://localhost:8080/goals)
//curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "description=Read about functional programming every /day" "http://localhost:8080/goals/"
app.post('/goals', function(req, res) {
var goal = {
description: req.body.description,
};
db.insert(goal, function(err, goal) {
if (err) res.send(err);
res.json(goal);
});
});
// GET a goal.
// (Accessed at GET http://localhost:8080/goals/goal_id)
app.get('/goals/:id', function(req, res) {
var goal_id = req.params.id;
db.findOne({
_id: goal_id
}, {}, function(err, goal) {
if (err) res.send(err);
res.json(goal);
});
});
// DELETE a goal.
// (Accessed at DELETE http://localhost:8080/goals/goal_id)
app.delete('/goals/:id', function(req, res) {
var goal_id = req.params.id;
db.remove({
_id: goal_id
}, {}, function(err, goal) {
if (err) res.send(err);
res.sendStatus(200);
});
});
// START THE SERVER
// ===============================================
app.listen(port, function()
{
console.log('Listening on port ' + port);
});