-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
60 lines (45 loc) · 1.55 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
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
var mysql = require('mysql');
var app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'bower_components')));
app.use(bodyParser.json());
var mysql = require('mysql'),
myConnection = require('express-myconnection'),
dbOptions = {
host : '127.0.0.1',
user : 'root',
password : '',
database : 'declutter',
};
app.use(myConnection(mysql, dbOptions, 'single'));
app.get('/endpoints/get_all_things.jsonp', function(req, res){
req.getConnection(function(err, connection) {
if (err) return next(err);
connection.query('SELECT * FROM things', [], function(err, results) {
if (err) return next(err);
res.send(
"angular.callbacks._0(\n"
+ JSON.stringify( results )
+ "\n);\n"
);
});
});
});
app.post('/endpoints/add_thing', function(req, res){
req.getConnection(function(err, connection) {
if (err) console.log(err);
connection.query(
'INSERT INTO things (`user_id`, `added_dt`, `name`, `description`) values (?, now(), ?, ?)',
[1, req.body.name, req.body.description],
function(err, results) {
if (err) console.log(err);
}
);
});
});
var server = app.listen(9000, function() {
console.log('Listening on port %d', server.address().port);
});