-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (46 loc) · 1.97 KB
/
index.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
// MODULES
const express = require('express')
const mongoose = require('mongoose')
const passport = require('passport')
const cookieSession = require('cookie-session')
// IMPORTS FROM OTHER MODULES
const authRouter = require('./routes/authRoutes')
const surveyRouter = require('./routes/surveyRoutes')
const billRouter = require('./routes/billRoutes')
const { mongoURI, cookieKey } = require('./config/keys')
require('./services/passport') // Setup passport config : Strategy, serialize and deserialize logic
if (process.env.NODE_ENV !== 'production') require('dotenv').config() // For link on '/'
// SET UP DATABASE
mongoose.connect(mongoURI, // Get the keys and set wire up the remote DB
{ useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true },
() => console.log("MDB Altas instance connected"))
// APP and COOKIE SETUP
const app = express() // Starting app instance on Server
app.use(express.json())
app.use(cookieSession({
maxAge: 30 * 24 * 60 * 60 * 1000, //30 days in ms
keys: [cookieKey]
}))
// Initialize and start passport session
app.use(passport.initialize())
app.use(passport.session())
app.use(authRouter) // Wire up the Auth flow
app.use(billRouter)
app.use(surveyRouter)
// https://devcenter.heroku.com/articles/nodejs-support#customizing-the-build-process
// Handle Production build
if (process.env.NODE_ENV === 'production') {
// Express will serve up production assets
// like main.js or main.css files
app.use(express.static('client/build')) // If searching for static assets
// It'll serve up index.html, if route is not understood
const path = require('path')
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client/build', 'index.html')) // concats it's params--> Absoulte path
})
}
// Get env from Heroku's Cloud env || assign 5000
// For Heroku deployment, add the engines prop with npm and node attributes set to version
const PORT = process.env.PORT || 5000
app.listen(PORT);
//nodemon index.js