2018-07-24 19:39:18 -04:00
|
|
|
const express = require('express');
|
|
|
|
const bodyParser = require('body-parser');
|
|
|
|
const methodOverride = require('method-override');
|
|
|
|
const mongoose = require('mongoose');
|
|
|
|
const morgan = require('morgan');
|
|
|
|
const passport = require('passport');
|
|
|
|
const session = require('express-session');
|
|
|
|
const sanitizer = require('express-sanitizer');
|
|
|
|
const helmet = require('helmet');
|
2017-10-09 22:01:02 -04:00
|
|
|
|
2018-07-24 19:39:18 -04:00
|
|
|
const app = express();
|
2017-10-09 22:01:02 -04:00
|
|
|
|
2018-07-24 19:39:18 -04:00
|
|
|
const config = require('config');
|
2017-10-12 17:26:35 -04:00
|
|
|
if(config.util.getEnv('NODE_ENV') !== 'test') {
|
|
|
|
app.use(morgan('combined'));
|
|
|
|
}
|
|
|
|
|
|
|
|
mongoose.Promise = global.Promise;
|
2018-07-25 01:42:39 -04:00
|
|
|
mongoose.connect(config.dbHost, {useNewUrlParser: true});
|
2018-07-24 19:39:18 -04:00
|
|
|
const db = mongoose.connection;
|
2017-10-09 22:01:02 -04:00
|
|
|
db.on('error', function(err) {
|
2017-10-12 17:26:35 -04:00
|
|
|
if (err) console.log('MongoDB Connection Error: ', err);
|
2017-10-09 22:01:02 -04:00
|
|
|
});
|
2018-07-24 19:39:18 -04:00
|
|
|
const MongoStore = require('connect-mongo')(session);
|
|
|
|
const mongoStore = new MongoStore({
|
2017-10-18 13:31:08 -04:00
|
|
|
url: config.dbHost
|
|
|
|
});
|
2017-10-09 22:01:02 -04:00
|
|
|
|
2017-10-11 10:15:19 -04:00
|
|
|
|
2017-10-18 13:31:08 -04:00
|
|
|
app.use(helmet());
|
|
|
|
app.set('trust proxy', 1);
|
|
|
|
app.use(session({
|
|
|
|
secret: 'secret',
|
|
|
|
name: 'session.id',
|
|
|
|
resave: false,
|
|
|
|
saveUninitialized: false,
|
|
|
|
store: mongoStore,
|
|
|
|
cookie: {
|
|
|
|
//secure: true,
|
|
|
|
httpOnly: true,
|
|
|
|
//domain: 'shimapan.rocks',
|
|
|
|
maxAge: 1000 * 60 * 60
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
app.use(passport.initialize());
|
|
|
|
app.use(passport.session());
|
2017-10-09 22:01:02 -04:00
|
|
|
app.use(bodyParser.json());
|
2018-07-24 19:39:18 -04:00
|
|
|
app.use(bodyParser.json({ type: 'application/*+json' }));
|
2017-10-09 22:01:02 -04:00
|
|
|
app.use(bodyParser.urlencoded({ extended: true }));
|
2017-10-12 17:26:35 -04:00
|
|
|
app.use(bodyParser.text());
|
2017-10-18 13:31:08 -04:00
|
|
|
app.use(sanitizer());
|
2017-10-09 22:01:02 -04:00
|
|
|
app.use(methodOverride('X-HTTP-Method-Override'));
|
|
|
|
|
2017-10-18 13:31:08 -04:00
|
|
|
|
|
|
|
//app.use(favicon(__dirname + '/public/img/favicon.ico'));
|
2017-10-09 22:01:02 -04:00
|
|
|
app.use(express.static(__dirname + '/public'));
|
2017-10-18 13:31:08 -04:00
|
|
|
|
|
|
|
|
2017-10-20 16:46:14 -04:00
|
|
|
require('./app/routes/routes.js')(app);
|
2017-10-18 13:31:08 -04:00
|
|
|
require('./config/passport.js');
|
|
|
|
|
2017-10-09 22:01:02 -04:00
|
|
|
|
|
|
|
// Start app
|
2018-07-24 19:39:18 -04:00
|
|
|
const port = process.env.PORT || 8080;
|
2018-07-25 01:42:39 -04:00
|
|
|
var server = app.listen(port);
|
2017-10-09 22:01:02 -04:00
|
|
|
console.log('Listening on port ', port, '...');
|
|
|
|
|
2018-07-24 19:39:18 -04:00
|
|
|
// Handle sigint
|
|
|
|
process.on('SIGINT', () => {
|
|
|
|
console.log('Shutting down...');
|
|
|
|
process.exitCode = 0;
|
|
|
|
});
|
|
|
|
|
2017-10-09 22:01:02 -04:00
|
|
|
// Expose app
|
2017-10-12 17:26:35 -04:00
|
|
|
exports.db = db;
|
|
|
|
exports.server = server;
|
|
|
|
exports.app = app;
|