-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
52 lines (41 loc) · 1.6 KB
/
server.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
const express = require('express');
const bodyParser = require('body-parser');
const session = require('express-session');
const MongoStore = require('connect-mongo')(session);
const cors = require('cors');
const app = express();
require('dotenv').config();
const PORT = process.env.PORT;
const routes = require('./routes');
// --------------------------------- Middleware --------------------------------- //
// CORS - Cross Origin Resource Sharing
const corsOptions = {
origin: [`http://localhost:3000`, 'https://wayfarer-ca.herokuapp.com'],
credentials: true, // allows the session cookie to be sent back and forth from server to client
optionsSuccessStatus: 200 // some legacy browsers choke on satus 204
};
app.use(cors(corsOptions));
// BodyParser
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
// Express Session - Authentication
app.use(session({
// Store session in DB
store: new MongoStore({ url: process.env.MONGODB_URI }),
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false, // Only create session if a propery has been added to session,
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 7 * 2, // Expire at 2 weeks
}
}));
// ----------------------------------- Routes ----------------------------------- //
app.get('/', (req, res) => {
res.send('<h1>Wayfarer</h1>');
});
app.use('/api/v1/auth', routes.auth);
app.use('/api/v1/users', routes.users);
app.use('/api/v1/posts', routes.posts);
app.use('/api/v1/cities', routes.cities);
app.use('/api/v1/comments', routes.comments);
app.listen(PORT, () => console.log(`Server connected at ${PORT}`));