const createError = require('http-errors'); const express = require('express'); const bodyParser = require('body-parser'); const path = require('path'); const cookieParser = require('cookie-parser'); const logger = require('morgan'); const MongoClient = require('mongodb').MongoClient; const assert = require('assert'); const cookieSession = require('cookie-session'); const apiRouter = require('./app/routes/api'); const authRouter = require('./app/routes/auth'); var app = express(); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'jade'); app.use(logger('dev')); app.use(express.json()); app.use(bodyParser.json()); app.use(express.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); app.use(cookieSession({ name: 'session', secret: 'flkasjgoeeovneogsfafjasdjfslkjlgjs', maxAge: 365 * 24 * 60 * 60 * 1000 // 1 year })); app.use('/api', apiRouter); app.use('/api/auth', authRouter); app.get('*', (req, res) => { res.sendFile(path.join(__dirname + '/client/build/index.html')); }); // catch 404 and forward to error handler app.use(function (req, res, next) { next(createError(404)); }); // error handler app.use(function (err, req, res, next) { // set locals, only providing error in development res.locals.message = err.message; res.locals.error = req.app.get('env') === 'development' ? err : {}; // render the error page res.status(err.status || 500); res.render('error'); }); function main() { console.log('startup dgdg'); const dbUrl = 'mongodb://localhost:27017'; const dbName = 'dgdg-dev'; let client = new MongoClient(dbUrl, { useUnifiedTopology: true }); client.connect(function (err) { assert.equal(null, err); app.locals.database = client.db(dbName); }); }; main(); module.exports = app;