-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
187 lines (155 loc) · 5.85 KB
/
server.js
File metadata and controls
187 lines (155 loc) · 5.85 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
///////////////////////
// Module Dependencies
///////////////////////
var express = require('express');
var http = require('http');
var app = new express();
var server = http.createServer(app);
var db = require('./database');
var config = require('./config');
///////////
// Globals
///////////
var application_root = __dirname;
var host = config.app.address;
var port = config.app.port;
console.log(host + " " + port);
/////////////
// App Setup
/////////////
app.configure(function() {
app.use(express.bodyParser()); // parses request body and populates req.body
app.use(express.methodOverride()); // check req.body for HTTP method overrides
app.use(app.router); // perform lookup based on url and HTTP method
app.use(express.static(application_root + '/public')); // where to get the static files
app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); // show all errors in development
});
// Start the server and connect to the database
server.listen(port, function() {
console.log("ExpressJS server started on @[%s], port [%d], in [%s] mode.", host, port, app.settings.env);
db.conn.connect();
});
////////////////////
// Setup the routes
////////////////////
//app.all('*', function(req, res, next){
// res.header("Access-Control-Allow-Origin", "*");
// res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
// res.header("Content-Type", "application/json");
// res.header("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS");
// next();
//});
app.get("/api", function(req, res) {
return res.send("API is running...");
});
app.get("/api/transactions", function(req, res) {
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
console.log(" Getting transactions for this month: " + month);
db.getByMonth(year, month, function(results) {
return res.send(results);
});
});
app.get("/api/transactions/all", function(req, res) {
console.log(" Getting ALL transactions");
db.getAll(function(results) {
return res.send(results);
});
});
app.get("/api/transactions/:year([0-9]+)", function(req, res) {
var year = req.params.year;
console.log(" Getting transactions for " + year + ".");
db.getByYear(year, function(results) {
return res.send(results);
});
});
app.get("/api/transactions/:year([0-9]+)/:month([0-9]+)", function(req, res) {
var year = req.params.year;
var month = req.params.month;
console.log(" Getting transactions for " + month + "/" + year);
db.getByMonth(year, month, function(results) {
return res.send(results);
});
});
app.get("/api/transactions/:year([0-9]+)/:month([0-9]+)/:day([0-9]+)", function(req, res) {
var year = req.params.year;
var month = req.params.month;
var day = req.params.day;
console.log(" Getting transactions for " + month + "/" + day + "/" + year + ".");
db.getByDay(year, month, day, function(results) {
return res.send(results);
});
});
app.get("/api/transactions/id/:id([0-9]+)", function(req, res) {
var id = req.params.id;
console.log(" Getting by ID: " + id);
db.getById(id, function(results) {
return res.send(results[0]);
});
});
app.get("/api/categories", function(req, res) {
console.log(" Grabbing categories");
db.getCategories(function(results) {
return res.send(results);
});
});
app.post("/api/transactions/id", function(req, res) {
console.log(" Inserting a transaction");
var transaction, date, description, category, amount = undefined;
transaction = req.body;
date = transaction.date;
year = date.substring(0,4);
month = date.substring(5,7);
day = date.substring(8,10);
description = transaction.description.replace(/'/g,"\\'");
category = (!transaction.category) ? "" : transaction.category;
amount = transaction.amount;
console.log(" DATE: " + date);
console.log(" year: " + year);
console.log(" month: " + month);
console.log(" day: " + day);
console.log(" DESCRIPTION: " + description);
console.log(" CATEGORY: " + category);
console.log(" AMOUNT: " + amount);
db.insert(year, month, day, description, category, amount, function(resultId) {
db.getById(resultId, function(results) {
return res.send(results);
});
});
});
app.put("/api/transactions/id/:id", function(req, res) {
console.log(" Updating a transaction");
var transaction, date, description, category, amount, id = undefined;
id = req.params.id;
transaction = req.body;
date = transaction.date;
year = date.substring(0,4);
month = date.substring(5,7);
day = date.substring(8,10);
description = transaction.description.replace(/'/g,"\\'");
category = (!transaction.category) ? "" : transaction.category;
amount = transaction.amount;
console.log(" ID: " + id);
console.log(" DATE: " + date);
console.log(" year: " + year);
console.log(" month: " + month);
console.log(" day: " + day);
console.log(" DESCRIPTION: " + description);
console.log(" CATEGORY: " + category);
console.log(" AMOUNT: " + amount);
db.update(id, year, month, day, description, category, amount, function(resultId) {
db.getById(resultId, function(results) {
return res.send(results);
});
});
});
app.delete("/api/transactions/id/:id", function(req, res) {
console.log(" Deleting a transaction");
var id = undefined;
id = req.params.id;
console.log(" ID: " + id);
db.delete(id, function(results) {
return res.send(results);
});
});