diff --git a/.ebextensions/static.config b/.ebextensions/static.config index d6f2514..18fedb3 100644 --- a/.ebextensions/static.config +++ b/.ebextensions/static.config @@ -1,4 +1,6 @@ option_settings: - namespace: aws:elasticbeanstalk:container:nodejs:staticfiles option_name: /public - value: /public \ No newline at end of file + value: /public + - option_name: NODE_ENV + value: production \ No newline at end of file diff --git a/app.js b/app.js index 72dedff..b2dc773 100644 --- a/app.js +++ b/app.js @@ -1,4 +1,3 @@ - /** * Module dependencies. */ @@ -6,33 +5,91 @@ 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(); + } +}); \ No newline at end of file diff --git a/package.json b/package.json index ae9468b..9e07b80 100644 --- a/package.json +++ b/package.json @@ -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": "*" } } \ No newline at end of file diff --git a/routes/hike.js b/routes/hike.js index fec29a9..bd8bca6 100644 --- a/routes/hike.js +++ b/routes/hike.js @@ -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){ }; \ No newline at end of file diff --git a/views/hike.jade b/views/hike.jade index ef7b09f..1db3c85 100644 --- a/views/hike.jade +++ b/views/hike.jade @@ -2,4 +2,21 @@ extends layout block content h1= title - p Welcome to #{title} \ No newline at end of file + 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} \ No newline at end of file