Skip to content

Commit

Permalink
Add database support
Browse files Browse the repository at this point in the history
  • Loading branch information
evandbrown committed Jul 11, 2013
1 parent d036794 commit c0b7b92
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 30 deletions.
4 changes: 3 additions & 1 deletion .ebextensions/static.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
option_settings:
- namespace: aws:elasticbeanstalk:container:nodejs:staticfiles
option_name: /public
value: /public
value: /public
- option_name: NODE_ENV
value: production
103 changes: 80 additions & 23 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,95 @@

/**
* Module dependencies.
*/

var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, hike = require('./routes/hike')
, http = require('http')
, path = require('path')
, hike = require('./routes/hike');
, mysql = require('mysql')
, async = require('async');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
// app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
// app.use(express.static(path.join(__dirname, 'public')));
});

app.get('/', routes.index);
app.get('/users', user.list);
app.get('/hikes', hike.index);
app.post('/add_hike', hike.add_hike);
app.configure('development', function() {
console.log('Using development settings.');
app.set('connection', mysql.createConnection({
host: '',
user: '',
port: '',
password: ''}));
app.use(express.errorHandler());
});

http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
app.configure('production', function() {
console.log('Using production settings.');
app.set('connection', mysql.createConnection({
host: process.env.RDS_HOSTNAME,
user: process.env.RDS_USERNAME,
password: process.env.RDS_PASSWORD,
port: process.env.RDS_PORT}));
});

function init() {
app.get('/', routes.index);
app.get('/users', user.list);
app.get('/hikes', hike.index);
app.post('/add_hike', hike.add_hike);

http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
}

var client = app.get('connection');
async.series([
function connect(callback) {
client.connect(callback);
},
function clear(callback) {
client.query('DROP DATABASE IF EXISTS mynode_db', callback);
},
function create_db(callback) {
client.query('CREATE DATABASE mynode_db', callback);
},
function use_db(callback) {
client.query('USE mynode_db', callback);
},
function create_table(callback) {
client.query('CREATE TABLE HIKES (' +
'ID VARCHAR(40), ' +
'HIKE_DATE DATE, ' +
'NAME VARCHAR(40), ' +
'DISTANCE VARCHAR(40), ' +
'LOCATION VARCHAR(40), ' +
'WEATHER VARCHAR(40), ' +
'PRIMARY KEY(ID))', callback);
},
function insert_default(callback) {
var hike = {HIKE_DATE: new Date(), NAME: 'Hazard Stevens',
LOCATION: 'Mt Rainier', DISTANCE: '4,027m vertical', WEATHER:'Bad'};
client.query('INSERT INTO HIKES set ?', hike, callback);
}
], function (err, results) {
if (err) {
console.log('Exception initializing database.');
throw err;
} else {
console.log('Database initialization complete.');
init();
}
});
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app.js"
"start": "node app"
},
"dependencies": {
"express": "3.3.4",
"jade": "*"
"express": "3.1.0",
"jade": "*",
"mysql": "*",
"async": "*",
"node-uuid": "*"
}
}
12 changes: 10 additions & 2 deletions routes/hike.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
var uuid = require('node-uuid');

exports.index = function(req, res) {
res.render('hike', {title: 'My Hiking Log'});
res.app.get('connection').query( 'SELECT * FROM HIKES', function(err, rows) {
if (err) {
res.send(err);
} else {
console.log(JSON.stringify(rows));
res.render('hike', {title: 'My Hiking Log', hikes: rows});
}});
};

exports.add_hike = function(req, res) {
exports.add_hike = function(req, res){
};
19 changes: 18 additions & 1 deletion views/hike.jade
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,21 @@ extends layout

block content
h1= title
p Welcome to #{title}
p Welcome to #{title}

div
h3 Hikes
table(border="1")
tr
td Date
td Name
td Location
td Distance
td Weather
each hike in hikes
tr
td #{hike.HIKE_DATE.toDateString()}
td #{hike.NAME}
td #{hike.LOCATION}
td #{hike.DISTANCE}
td #{hike.WEATHER}

0 comments on commit c0b7b92

Please sign in to comment.