1
0
mirror of https://github.com/Foltik/Shimapan synced 2024-11-28 05:26:44 -05:00
shimapan/server.js

84 lines
2.2 KiB
JavaScript
Raw Normal View History

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
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);
});
// MongoDB
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');
db = mongoose.connection;
});
const MongoStore = require('connect-mongo')(session);
const mongoStore = new MongoStore({url: dbHost});
2017-10-09 22:01:02 -04:00
// HTTP Request Logging
app.use(morgan(config.get('Log.httpLevel')));
2017-10-11 10:15:19 -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
}
}));
// 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());
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'));
// 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
// 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
const port = config.get('Server.port');
const server = app.listen(port, () => {
console.log('Listening on port ' + port + '...\n');
});
2017-10-09 22:01:02 -04:00
// Expose app
module.exports.app = app;
module.exports.server = server;