-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
69 lines (55 loc) · 2 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
require('dotenv').config();
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const express = require('express');
const mongoose = require('mongoose');
const logger = require('morgan');
const cors = require("cors");
const session = require("express-session");
const MongoStore = require("connect-mongo")(session);
const path = require("path");
const passportSetup = require("./config/passport/passport-setup.js");
mongoose
.connect(process.env.MONGODB_URI, {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 app = express();
// Middleware Setup
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
// Allow Cross-Origin Resource Sharing (cors)
// (access the API from the frontend JavaScript on a different domain/origin)
app.use(cors({
// allow other domains/origins to send cookies
credentials: true,
// this is the domain we want cookies from (our React app)
origin: [ "http://localhost:3000" ]
}));
// Session setup AFTER CORS
app.use(session({
secret: "qwertyuiop1234567890",
saveUninitialized: true,
resave: true,
store: new MongoStore({ mongooseConnection: mongoose.connection }),
}));
// Passport setup AFTER SESSION
passportSetup(app);
const phoneRouter = require("./routes/phone-router.js");
app.use("/api", phoneRouter);
const authRouter = require("./routes/auth-router.js");
app.use("/api", authRouter);
const fileRouter = require("./routes/file-router.js");
app.use("/api", fileRouter);
// Send React's HTML for all other routes (they might be React routes)
// 😟 SIDE EFFECT: JSON 404 page won't work anymore
app.use((req, res, next) => {
res.sendFile(path.join(__dirname, "public", "index.html"));
});
module.exports = app;