import cfenv from 'cfenv'; import express from 'express'; import mongoose from 'mongoose'; import swagger from 'swagger-ui-express'; import yaml from 'yamljs'; // tell if server is running in debug or production environment console.log(process.env.NODE_ENV === 'production' ? '===== PRODUCTION =====' : '===== DEVELOPMENT ====='); // get mongodb address from server, otherwise set to localhost let connectionString: string = ""; if(process.env.NODE_ENV === 'production') { let services = cfenv.getAppEnv().getServices(); for (let service in services) { if(services[service].tags.indexOf("mongodb") >= 0) { connectionString = services[service]["credentials"].uri; } } } else { connectionString = 'mongodb://localhost/dfopdb'; } mongoose.connect(connectionString, {useNewUrlParser: true, useUnifiedTopology: true}); // connect to mongodb let db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); db.once('open', () => { console.log(`Connected to ${connectionString}`); }); // create Express app const app = express(); app.disable('x-powered-by'); // get port from environment, defaults to 3000 const port = process.env.PORT || 3000; // require routes app.use('/', require('./routes/root')); // Swagger UI app.use('/api', swagger.serve, swagger.setup( yaml.load('./oas.yaml'), { defaultModelsExpandDepth: -1, customCss: '.swagger-ui .topbar { display: none }' } ) ); // hook up server to port app.listen(port, () => { console.log(`Listening on http;//localhost:${port}`); });