This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
95 lines (72 loc) · 2.46 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
require('dotenv').config();
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const express = require('express');
// const favicon = require('serve-favicon');
const mongoose = require('mongoose');
const logger = require('morgan');
const path = require('path');
const flash = require('connect-flash');
const session = require('express-session');
const passport = require('passport');
require('./configs/passport');
const cors = require('cors');
// Enable authentication using session + passport
const MongoStore = require('connect-mongo')(session);
const appName = require('./package.json').name;
mongoose
.connect(process.env.MONGODB_URI || 'mongodb://localhost/gardengnome', { useNewUrlParser: true })
.then(x => {
console.log(
`Connected to Mongo! Database name: "${x.connections[0].name}"`
);
})
.catch(err => {
console.error('Error connecting to mongo', err);
});
const debug = require('debug')(
`${appName}:${path.basename(__filename).split('.')[0]}`
);
const app = express();
// Middleware Setup
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
// default value for title local
app.locals.title = 'Garden Gnome server';
app.use(express.static(path.join(__dirname, "/client/build"))); // path.join(__dirname, 'public')
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true,
store: new MongoStore({ mongooseConnection: mongoose.connection }),
})
);
app.use(flash());
// USE passport.initialize() and passport.session() HERE:
app.use(passport.initialize());
app.use(passport.session());
// USE CORS to allow React to run through different PORT in conjunction
app.use(
cors({
credentials: true,
origin: ['http://localhost:3000'], // <== this will be the URL of our React app (it will be running on port 3000)
})
);
// ROUTES MIDDLEWARE STARTS HERE:
const index = require('./routes/index');
app.use('/', index);
const authRoutes = require('./routes/auth');
app.use('/api/auth', authRoutes);
const plantRoutes = require('./routes/plants');
app.use('/api/plants', plantRoutes);
const userRoutes = require('./routes/user');
app.use('/api/user', userRoutes);
app.use('/api', require('./routes/file-upload-routes'));
app.use((req, res) => {
// If no routes match, send them the React HTML.
res.sendFile(__dirname + "/client/build/index.html");
});
module.exports = app;