-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
69 lines (59 loc) · 1.83 KB
/
main.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
var express = require('express');
var mysql = require('./dbcon.js');
var app = express();
var handlebars = require('express-handlebars').create({defaultLayout:'main'});
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');
app.set('port', 7957);
app.get('/',function(req,res,next){
var context = {};
mysql.pool.query('SELECT * FROM workouts', function(err, rows, fields){
if(err){
next(err);
return;
}
context.results = JSON.stringify(rows);
res.render('home', context);
});
});
app.get('/update', function(req,res,next){
var context = {};
mysql.pool.query("UPDATE workouts SET name=?, reps=?, weight=?, date=?, pounds=? WHERE id = ?",
[req.query.name, req.query.reps, req.query.weight, req.query.date, req.query.pounds],
function(err, result){
if(err){
next(err);
return;
}
context.results = "Updated " + result.changedRows + " rows.";
res.render('home',context);
});
});
app.get('/reset-table',function(req,res,next){
var context = {};
mysql.pool.query("DROP TABLE IF EXISTS workouts", function(err){ //replace your connection pool with the your variable containing the connection pool
var createString = "CREATE TABLE workouts("+
"id INT PRIMARY KEY AUTO_INCREMENT,"+
"name VARCHAR(255) NOT NULL,"+
"reps INT,"+
"weight INT,"+
"date DATE,"+
"lbs BOOLEAN)";
mysql.pool.query(createString, function(err){
context.results = "Table reset";
res.render('home',context);
})
});
});
app.use(function(req,res){
res.status(404);
res.render('404');
});
app.use(function(err, req, res, next){
console.error(err.stack);
res.status(500);
res.render('500');
});
app.listen(app.get('port'), function(){
console.log('Express started on http://localhost:' + app.get('port') + '; press Ctrl-C to terminate.');
});