-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
90 lines (81 loc) · 2.14 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
var express = require('express'),
bodyParser = require('body-parser'),
jsendie = require('jsendie'),
path = require('path'),
app = express(),
Task = require('./app/models/task'),
PORT = process.env.PORT || 3000;
// view engine setup
app.set('views', path.join(__dirname, 'app/views'));
app.set('view engine', 'ejs');
// middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
app.use(jsendie());
// single page todo app
app.get('/', function(req, res, next){
res.render('index');
});
// create task
app.post('/task', function(req, res, next){
var b = req.body;
Task.forge({
title: b.title,
completed: b.completed
}).save().then(function (task) {
res.success(task);
}).catch(function (err) {
console.error('Error saving record');
console.error(err);
res.error(err);
});
});
// retrieve all tasks
app.get('/task', function(req, res, next){
Task.fetchAll().then(function(tasks){
res.success(tasks);
}).catch(function (err) {
console.error('Error retrieve record');
console.error(err);
res.error(err);
});
});
// update task
app.put('/task/:id', function(req, res, next){
var p = req.params;
var b = req.body;
Task.forge({
id: p.id
}).fetch().then(function(task){
return task.save({
title: b.title,
completed: b.completed
});
}).then(function(saved){
res.success(saved);
}).catch(function (err){
console.error('Error update record');
console.error(err);
res.error(err);
});
});
// delete task
app.delete('/task/:id', function(req, res, next){
var p = req.params;
Task.forge({
id: p.id
}).fetch().then(function(task){
return task.destroy();
}).then(function(){
res.success('Task deleted');
}).catch(function(err){
console.error('Error delete record');
console.error(err);
res.error(err);
});
});
app.listen(PORT, function(){
console.log('Server listen to port: ' + PORT);
});
module.exports = app;