-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
83 lines (68 loc) · 2.69 KB
/
server.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
///////////////////////////////////////////////////////////
// Import modules
///////////////////////////////////////////////////////////
const path = require("path")
const express = require("express")
const cors = require("cors")
const bodyParser = require("body-parser")
const mongoose = require("mongoose")
const passport = require("passport")
const session = require("express-session")
const MongoStore = require("connect-mongo")(session)
const favicon = require("serve-favicon")
require("dotenv").config({path: path.resolve(__dirname, ".env"), })
///////////////////////////////////////////////////////////
// Configure and connect to MongoDB database
///////////////////////////////////////////////////////////
const dbconf = require("./db.js")
mongoose.Promise = global.Promise
mongoose
.connect(dbconf.url, dbconf.options)
.catch(({message,}) => {
console.error(`Unable to connect to the mongodb instance: ${message}`)
})
const db = mongoose.connection
db.on("error", ({message,}) => {
console.error(`Mongoose default connection error: ${message}`)
})
db.once("open", () => {
console.info(`Mongoose default connection opened [${dbconf.db}]`)
})
///////////////////////////////////////////////////////////
// Initialize Express and configure Middleware
///////////////////////////////////////////////////////////
const app = express()
app.set("port", process.env.PORT || 3000)
// Serve static files
app.use(express.static(path.join(__dirname, "public")))
app.use(favicon(path.resolve(__dirname, "public", "images", "favicon.ico")))
// Body parsing - parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false, }))
// Body parsing - parse json
app.use(bodyParser.json())
// Handle cross-site request
app.use(cors())
// Initialize Passport and enable persistent login sessions stored in mongodb
const sessionOptions = {
secret: "cmccormack-nightlife-app",
resave: false,
saveUninitialized: true,
cookie: { secure: false, },
store: new MongoStore({ mongooseConnection: db, }),
}
app.use(session(sessionOptions))
app.use(passport.initialize())
app.use(passport.session())
require("./auth/init")(app, passport)
require("./auth/twitter")(app, passport)
///////////////////////////////////////////////////////////
// Import Express Routes and call with Express app
///////////////////////////////////////////////////////////
require("./routes/routes.js")(app, passport)
///////////////////////////////////////////////////////////
// Start Express Server
///////////////////////////////////////////////////////////
const server = app.listen(app.get("port"), () => {
const {port, address, } = server.address()
console.info(`Express server started on ${address}:${port}`)
})