Created
December 7, 2015 14:28
-
-
Save Kikketer/5e671d845caa70d2ccc1 to your computer and use it in GitHub Desktop.
Deployd database wiped on startup
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
// Set default node environment to development | |
process.env.NODE_ENV = process.env.NODE_ENV || 'development'; | |
var express = require('express'); | |
var mongoose = require('mongoose'); | |
// Returns some config objects, but I'm not using most for the Deployd test | |
var config = require('./config/environment'); | |
// Setup server | |
var app = express(); | |
var server = require('http').createServer(app); | |
// Most of the "app.use" stuff is in here | |
require('./config/express')(app); | |
// Add the deployd sockets | |
var io = require('socket.io').listen(server, {'log level': 0}); | |
require('deployd').attach(server, { | |
socketIo: io, // if not provided, attach will create one for you. | |
env: process.env.NODE_ENV, | |
db: { | |
host: 'localhost', | |
port: 27017, | |
name: 'test-conf' | |
} | |
}); | |
app.use(server.handleRequest); | |
// Start server | |
server.listen(config.port, function() { | |
console.log('Express server listening on %d, in %s mode', config.port, app.get('env')); | |
}); | |
// Expose app | |
exports = module.exports = app; |
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
'use strict'; | |
// Development specific configuration | |
// ================================== | |
module.exports = { | |
// MongoDB connection options | |
// I'm not using this for the Deployd connection, but wanted to show all of the files and what they are doing | |
mongo: { | |
uri: 'mongodb://localhost/test-conf' | |
}, | |
seedDB: true | |
}; |
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
'use strict'; | |
var path = require('path'); | |
var _ = require('lodash'); | |
function requiredProcessEnv(name) { | |
if(!process.env[name]) { | |
throw new Error('You must set the ' + name + ' environment variable'); | |
} | |
return process.env[name]; | |
} | |
// All configurations will extend these options | |
// ============================================ | |
var all = { | |
env: process.env.NODE_ENV, | |
// Root path of server | |
root: path.normalize(__dirname + '/../../..'), | |
// Server port | |
port: process.env.VCAP_APP_PORT || 3000, | |
// Should we populate the DB with sample data? | |
seedDB: false, | |
// Secret for session, you will want to change this and make it an environment variable | |
secrets: { | |
session: 'test-conf-secret' | |
}, | |
// List of user roles | |
userRoles: ['guest', 'user', 'admin'], | |
// MongoDB connection options | |
mongo: { | |
options: { | |
db: { | |
safe: true | |
} | |
} | |
} | |
}; | |
// Export the config object based on the NODE_ENV | |
// ============================================== | |
module.exports = _.merge( | |
all, | |
require('./' + process.env.NODE_ENV + '.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
/** | |
* Express configuration | |
*/ | |
'use strict'; | |
var express = require('express'); | |
var favicon = require('serve-favicon'); | |
var morgan = require('morgan'); | |
var compression = require('compression'); | |
var bodyParser = require('body-parser'); | |
var methodOverride = require('method-override'); | |
var cookieParser = require('cookie-parser'); | |
var errorHandler = require('errorhandler'); | |
var path = require('path'); | |
var config = require('./environment'); | |
module.exports = function(app) { | |
var env = app.get('env'); | |
app.set('views', config.root + '/server/views'); | |
app.engine('html', require('ejs').renderFile); | |
app.set('view engine', 'html'); | |
app.use(compression()); | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.use(bodyParser.json()); | |
app.use(methodOverride()); | |
app.use(cookieParser()); | |
app.use(function(req, res, next) { | |
res.header('Access-Control-Allow-Origin', "*"); | |
res.header('Access-Control-Allow-Headers', 'Content-Type, X-Requested-With, Authorization'); | |
res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, OPTIONS'); | |
if (req.method === 'OPTIONS') { | |
res.send(200); | |
} | |
else { | |
next(); | |
} | |
}); | |
if ('production' === env) { | |
app.use(favicon(path.join(config.root, 'dist', 'favicon.ico'))); | |
app.use(express.static(path.join(config.root, 'dist'))); | |
app.set('appPath', 'dist'); | |
app.use(morgan('dev')); | |
} | |
if ('development' === env || 'test' === env) { | |
app.use(favicon(path.join(config.root, 'client', 'favicon.ico'))); | |
app.use('/.tmp', express.static(path.join(config.root, '.tmp'))); | |
app.use(express.static(path.join(config.root, 'client'))); | |
app.use('/bower_components', express.static(path.join(config.root, 'bower_components'))); | |
app.set('appPath', 'client'); | |
app.use(morgan('dev')); | |
app.use(errorHandler()); // Error handler - has to be last | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment