-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
61 lines (48 loc) · 1.58 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
54
55
56
57
58
59
60
/* eslint-disable import/no-unassigned-import */
require('dotenv').config();
const path = require('path');
const express = require('express');
const mongoose = require('mongoose');
const morgan = require('morgan');
const passport = require('passport');
// Load models before router
require('./models/song');
require('./models/playlist');
require('./models/user');
const app = express();
const port = process.env.PORT || 3000;
const dbUrl = process.env.ENV === 'prod' ? process.env.DB_PROD_URL : process.env.DB_LOCAL_URL;
const session = require('express-session');
const bodyParser = require('body-parser');
const {authenticate} = require('./middleware/auth');
mongoose.connect(dbUrl)
.then(() => {
console.log('Connected DB successfully', dbUrl);
})
.catch(err => {
console.log(err);
});
require('./middleware/passport')(passport);
app.use(morgan('combined'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.set('view engine', 'ejs'); // Set up ejs for templating
app.use(session({
secret: 'musicalhub',
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
require('./routes/auth')(app, passport);
app.use('/api/search', require('./routes/search'));
app.use('/api', authenticate);
app.use('/api/playlist', require('./routes/playlist'));
// Serve react app
app.use(express.static(path.resolve(__dirname, 'client', 'build')));
app.get('/', function(req, res) {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
app.listen(port, () => {
console.log('Listening port ' + port);
});