Created
April 17, 2014 17:54
-
-
Save ryanburnett/11001173 to your computer and use it in GitHub Desktop.
server.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'), | |
stylus = require('stylus'); | |
mongoose = require('mongoose'); | |
var env = process.env.NODE_ENV = process.env.NODE_ENV || 'development'; | |
var app = express(); | |
function compile(str, path) { | |
return stylus(str).set('filename', path); | |
} | |
app.configure(function(){ | |
app.set('views', __dirname + '/server/views'); | |
app.set('view engine', 'jade'); | |
app.use(express.logger('dev')); | |
app.use(express.bodyParser()); | |
app.use(stylus.middleware( | |
{ | |
src: __dirname + '/public', | |
compile: compile | |
} | |
)); | |
app.use(express.static(__dirname + '/public')); | |
}); | |
if(env === 'development') { | |
// Connect to localhost db | |
mongoose.connect('mongodb://localhost/multivision'); | |
} else { | |
// Connect to mongolab db | |
mongolab connection string | |
} | |
var db = mongoose.connection; | |
db.on('error', console.error.bind(console, 'connection error...')); | |
db.once('open', function callback() { | |
console.log('multivision db opened'); | |
}); | |
var messageSchema = mongoose.Schema({message: String}); | |
var Message = mongoose.model('Message', messageSchema); | |
var mongoMessage; | |
Message.findOne().exec(function(err, messageDoc) { | |
mongoMessage = messageDoc.message; | |
}); | |
app.get('/partials/:partialPath', function(req, res) { | |
res.render('partials/' + req.params.partialPath); | |
}); | |
app.get('*', function(req, res){ | |
res.render('index', { | |
mongoMessage: mongoMessage | |
}); | |
}); | |
var port = process.env.PORT || 3030; | |
app.listen(port); | |
console.log('Listening on port ' + port + '...'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment