Skip to content

Commit c0b7b92

Browse files
committed
Add database support
1 parent d036794 commit c0b7b92

File tree

5 files changed

+117
-30
lines changed

5 files changed

+117
-30
lines changed

.ebextensions/static.config

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
option_settings:
22
- namespace: aws:elasticbeanstalk:container:nodejs:staticfiles
33
option_name: /public
4-
value: /public
4+
value: /public
5+
- option_name: NODE_ENV
6+
value: production

app.js

Lines changed: 80 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,95 @@
1-
21
/**
32
* Module dependencies.
43
*/
54

65
var express = require('express')
76
, routes = require('./routes')
87
, user = require('./routes/user')
8+
, hike = require('./routes/hike')
99
, http = require('http')
1010
, path = require('path')
11-
, hike = require('./routes/hike');
11+
, mysql = require('mysql')
12+
, async = require('async');
1213

1314
var app = express();
1415

15-
// all environments
16-
app.set('port', process.env.PORT || 3000);
17-
app.set('views', __dirname + '/views');
18-
app.set('view engine', 'jade');
19-
app.use(express.favicon());
20-
app.use(express.logger('dev'));
21-
app.use(express.bodyParser());
22-
app.use(express.methodOverride());
23-
app.use(app.router);
24-
// app.use(express.static(path.join(__dirname, 'public')));
25-
26-
// development only
27-
if ('development' == app.get('env')) {
28-
app.use(express.errorHandler());
29-
}
16+
app.configure(function(){
17+
app.set('port', process.env.PORT || 3000);
18+
app.set('views', __dirname + '/views');
19+
app.set('view engine', 'jade');
20+
app.use(express.favicon());
21+
app.use(express.logger('dev'));
22+
app.use(express.bodyParser());
23+
app.use(express.methodOverride());
24+
app.use(app.router);
25+
// app.use(express.static(path.join(__dirname, 'public')));
26+
});
3027

31-
app.get('/', routes.index);
32-
app.get('/users', user.list);
33-
app.get('/hikes', hike.index);
34-
app.post('/add_hike', hike.add_hike);
28+
app.configure('development', function() {
29+
console.log('Using development settings.');
30+
app.set('connection', mysql.createConnection({
31+
host: '',
32+
user: '',
33+
port: '',
34+
password: ''}));
35+
app.use(express.errorHandler());
36+
});
3537

36-
http.createServer(app).listen(app.get('port'), function(){
37-
console.log('Express server listening on port ' + app.get('port'));
38+
app.configure('production', function() {
39+
console.log('Using production settings.');
40+
app.set('connection', mysql.createConnection({
41+
host: process.env.RDS_HOSTNAME,
42+
user: process.env.RDS_USERNAME,
43+
password: process.env.RDS_PASSWORD,
44+
port: process.env.RDS_PORT}));
3845
});
46+
47+
function init() {
48+
app.get('/', routes.index);
49+
app.get('/users', user.list);
50+
app.get('/hikes', hike.index);
51+
app.post('/add_hike', hike.add_hike);
52+
53+
http.createServer(app).listen(app.get('port'), function(){
54+
console.log("Express server listening on port " + app.get('port'));
55+
});
56+
}
57+
58+
var client = app.get('connection');
59+
async.series([
60+
function connect(callback) {
61+
client.connect(callback);
62+
},
63+
function clear(callback) {
64+
client.query('DROP DATABASE IF EXISTS mynode_db', callback);
65+
},
66+
function create_db(callback) {
67+
client.query('CREATE DATABASE mynode_db', callback);
68+
},
69+
function use_db(callback) {
70+
client.query('USE mynode_db', callback);
71+
},
72+
function create_table(callback) {
73+
client.query('CREATE TABLE HIKES (' +
74+
'ID VARCHAR(40), ' +
75+
'HIKE_DATE DATE, ' +
76+
'NAME VARCHAR(40), ' +
77+
'DISTANCE VARCHAR(40), ' +
78+
'LOCATION VARCHAR(40), ' +
79+
'WEATHER VARCHAR(40), ' +
80+
'PRIMARY KEY(ID))', callback);
81+
},
82+
function insert_default(callback) {
83+
var hike = {HIKE_DATE: new Date(), NAME: 'Hazard Stevens',
84+
LOCATION: 'Mt Rainier', DISTANCE: '4,027m vertical', WEATHER:'Bad'};
85+
client.query('INSERT INTO HIKES set ?', hike, callback);
86+
}
87+
], function (err, results) {
88+
if (err) {
89+
console.log('Exception initializing database.');
90+
throw err;
91+
} else {
92+
console.log('Database initialization complete.');
93+
init();
94+
}
95+
});

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
"version": "0.0.1",
44
"private": true,
55
"scripts": {
6-
"start": "node app.js"
6+
"start": "node app"
77
},
88
"dependencies": {
9-
"express": "3.3.4",
10-
"jade": "*"
9+
"express": "3.1.0",
10+
"jade": "*",
11+
"mysql": "*",
12+
"async": "*",
13+
"node-uuid": "*"
1114
}
1215
}

routes/hike.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
var uuid = require('node-uuid');
2+
13
exports.index = function(req, res) {
2-
res.render('hike', {title: 'My Hiking Log'});
4+
res.app.get('connection').query( 'SELECT * FROM HIKES', function(err, rows) {
5+
if (err) {
6+
res.send(err);
7+
} else {
8+
console.log(JSON.stringify(rows));
9+
res.render('hike', {title: 'My Hiking Log', hikes: rows});
10+
}});
311
};
412

5-
exports.add_hike = function(req, res) {
13+
exports.add_hike = function(req, res){
614
};

views/hike.jade

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,21 @@ extends layout
22

33
block content
44
h1= title
5-
p Welcome to #{title}
5+
p Welcome to #{title}
6+
7+
div
8+
h3 Hikes
9+
table(border="1")
10+
tr
11+
td Date
12+
td Name
13+
td Location
14+
td Distance
15+
td Weather
16+
each hike in hikes
17+
tr
18+
td #{hike.HIKE_DATE.toDateString()}
19+
td #{hike.NAME}
20+
td #{hike.LOCATION}
21+
td #{hike.DISTANCE}
22+
td #{hike.WEATHER}

0 commit comments

Comments
 (0)