-
Notifications
You must be signed in to change notification settings - Fork 14
/
server.js
55 lines (47 loc) · 1.28 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
var
express = require("express"),
path = require("path"),
nedb = require('nedb'),
databaseUrl = "db/items.db";
var db = {
items: new nedb({ filename: databaseUrl, autoload: true })
};
var app = express();
app.configure(function () {
app.set('port', process.env.PORT || 3000);
app.use(express.logger('dev'));
app.use(express.bodyParser()),
app.use(express.static(path.join(__dirname, 'public')));
});
app.get('/api', function (req, res) {
res.send('API is running');
});
app.get('/items', function (req, res) {
db.items.find({}, function(err, result) {
res.send(result);
});
});
app.post('/items', function (req, res) {
var item = req.body;
db.items.insert(item, function (err, result) {
if (err) {
res.send({'error':'An error has occurred'});
} else {
console.log('Success: ' + JSON.stringify(result));
res.send(result);
}
});
});
app.delete('/items/:id', function (req, res) {
var id = req.params.id;
db.items.remove({_id: id}, {}, function (err, result) {
if (err) {
res.send({'error':'An error has occurred - ' + err});
} else {
console.log('' + result + ' document(s) deleted');
res.send(req.body);
}
});
});
app.listen(app.get('port'));
console.log('Server listening on port ' + app.get('port'));