-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.js
53 lines (50 loc) · 1.45 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const express = require('express')
const app = express();
const bodyParser = require('body-parser')
const session = require('express-session')
const MongoStore = require('connect-mongo')(session);
const path = require('path')
const morgan = require('morgan');
const config = require('./config/config')
const port = process.env.PORT || 3000
const mongo = require('mongodb');
const objectId = mongo.ObjectId;
const MongoClient = mongo.MongoClient;
const dbURL = config.production.database
var DBObj
app.use(session({
secret: 'randomsecret',
store: new MongoStore({ url: dbURL }),
saveUninitialized: false,
resave: false
}));
MongoClient.connect(dbURL, (err, db) => {
if (err) {
console.log('DB could not be connected\n', err.message)
return
}
DBObj = db
app.listen(port, function() {
console.log(`App listening on port: ${port}`)
});
})
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(morgan('dev'));
const web_pages = require('./routes/web_pages')
const users = require('./routes/accounts')
const apis = require('./routes/apis')
//This middleware enables the db variable to be used in all routes
app.use(function(req, res, next) {
req.objectId = objectId
req.db = DBObj;
next();
});
app.use('/', web_pages)
app.use('/users', users)
app.use('/apis', apis)