-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
66 lines (59 loc) · 2.03 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
import express from 'express';
import mongoose from 'mongoose';
import cors from 'cors';
import session from "express-session";
import crypto from "crypto";
import YelpController from './controllers/yelp-controller.js';
import RestaurantsController from './controllers/restaurants-controller.js';
import RatingsController from './restaurants/ratings/ratings-controller.js'
import ReviewsController from './controllers/reviews-controller.js';
import UsersController from "./users/users-controller.js";
import FollowController from "./follow/follow-controller.js";
// Set "production" to true if running on servers; set to false if running on localhost:
const production = true;
// Connecting to MongoDB:
const CONNECTION_STRING = process.env.DB_CONNECTION_STRING || 'mongodb://127.0.0.1:27017/finalproject';
const MONGOOSE_CONNECT_OPTIONS = {
useNewUrlParser: true,
useUnifiedTopology: true,
serverSelectionTimeoutMS: 5000,
autoIndex: false, // do not build indexes
maxPoolSize: 10, // maintain up to 10 socket connections
socketTimeoutMS: 45000, // close sockets after 45 seconds of inactivity
family: 4 // use IPv4, skip trying IPv6
}
mongoose.connect(CONNECTION_STRING, MONGOOSE_CONNECT_OPTIONS);
// Configuring cors to support cookies (and restricting network access from React app):
const app = express();
app.use(
cors(
{
credentials: true,
origin: true
}
)
);
// Configuring session library:
if (production) {
app.set("trust proxy", 1);
}
const secret = crypto.randomBytes(32).toString("hex");
let sess = {
secret: secret,
saveUninitialized: true,
resave: true,
cookie: {
sameSite: production ? "none" : "lax",
secure: production // needs HTTPS
}
};
app.use(session(sess));
// Parse JSON from HTTP request body:
app.use(express.json());
YelpController(app);
RestaurantsController(app);
RatingsController(app);
ReviewsController(app);
UsersController(app);
FollowController(app);
app.listen(process.env.PORT || 4000);