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();
|
|
|
|
const config = require('config');
|
2017-10-12 17:26:35 -04:00
|
|
|
|
2018-07-29 19:35:58 -04:00
|
|
|
process.on('uncaughtException', function (err) {
|
|
|
|
console.log('FATAL UNCAUGHT EXCEPTION:');
|
|
|
|
console.log(err.stack);
|
|
|
|
});
|
|
|
|
|
2018-07-25 10:18:29 -04:00
|
|
|
// MongoDB
|
2018-07-26 17:34:47 -04:00
|
|
|
const dbHost = config.get('Database.host');
|
|
|
|
let db;
|
|
|
|
mongoose.connect(dbHost, {useNewUrlParser: true})
|
|
|
|
.then(() => {
|
2018-07-29 19:35:58 -04:00
|
|
|
console.log('Connected to database ' + dbHost + '\n');
|
2018-07-26 17:34:47 -04:00
|
|
|
db = mongoose.connection;
|
|
|
|
});
|
|
|
|
|
2018-07-24 19:39:18 -04:00
|
|
|
const MongoStore = require('connect-mongo')(session);
|
2018-07-26 17:34:47 -04:00
|
|
|
const mongoStore = new MongoStore({url: dbHost});
|
2017-10-09 22:01:02 -04:00
|
|
|
|
2018-07-25 10:18:29 -04:00
|
|
|
// HTTP Request Logging
|
2018-07-26 17:34:47 -04:00
|
|
|
app.use(morgan(config.get('Log.httpLevel')));
|
2017-10-11 10:15:19 -04:00
|
|
|
|
2018-07-25 10:18:29 -04:00
|
|
|
// Session setup
|
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
|
|
|
|
}
|
|
|
|
}));
|
2018-07-25 10:18:29 -04:00
|
|
|
|
|
|
|
// Middleware
|
2018-07-26 19:01:16 -04:00
|
|
|
app.use(passport.initialize(null));
|
|
|
|
app.use(passport.session(null));
|
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'));
|
|
|
|
|
2018-07-25 10:18:29 -04:00
|
|
|
// Static directories and favicon
|
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
|
|
|
|
2018-07-25 10:18:29 -04:00
|
|
|
// Install routes and configure authentication strategy
|
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');
|
|
|
|
|
2018-07-25 18:43:20 -04:00
|
|
|
// Error handler
|
2018-07-26 19:01:16 -04:00
|
|
|
app.use((err, req, res) => {
|
2018-07-25 18:43:20 -04:00
|
|
|
console.error(err.stack);
|
|
|
|
res.status(500).json({'message': 'Internal server error.'});
|
|
|
|
});
|
|
|
|
|
2017-10-09 22:01:02 -04:00
|
|
|
// Start app
|
2018-07-26 17:34:47 -04:00
|
|
|
const port = config.get('Server.port');
|
2018-07-26 13:16:42 -04:00
|
|
|
const server = app.listen(port, () => {
|
|
|
|
console.log('Listening on port ' + port + '...\n');
|
2018-07-24 19:39:18 -04:00
|
|
|
});
|
|
|
|
|
2017-10-09 22:01:02 -04:00
|
|
|
// Expose app
|
2018-07-26 13:16:42 -04:00
|
|
|
module.exports.app = app;
|
|
|
|
module.exports.server = server;
|