const express = require('express');
const config = require('config');
const mongoose = require('mongoose');
const cors = require('cors');
const bodyParser = require('body-parser');
const app = express();
const expressWs = require('express-ws')(app);
const path = require('path');
const fs = require('fs');
app.ws('*', function(ws, req) {
ws.send('q')
});
const jsonParser = bodyParser.json()
const key = fs.readFileSync('ssl/private.key');
const cert = fs.readFileSync('ssl/certificate.crt');
const ca = fs.readFileSync('ssl/ca_bundle.crt');
const options = {
key: key,
cert: cert,
ca: ca
}
const https = require('https');
https.createServer(options, app).listen(443)
app.use(cors());
app.use (function (req, res, next) {
if (req.secure) {
next();
} else {
res.redirect('https://' + req.headers.host + req.url);
}
});
if (process.env.NODE_ENV === 'production') {
app.use('/', express.static(path.join(__dirname, 'client', 'build')));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'))
})
}
const PORT = config.get('port') || 5000;
async function start() {
try {
await mongoose.connect(config.get('mongoURI'), {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
});
app.listen(PORT, ()=> console.log(`App has been started on port ${PORT}...`));
} catch (e) {
console.log('Server Error', e.message);
process.exit(1);
}
}
start();