-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
61 lines (50 loc) · 1.63 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
53
54
55
56
57
58
59
60
61
// Imports
const path = require('path');
const cors = require('cors');
const connectToDB = require('./db.js');
const express = require('express');
const session = require('express-session');
const MongoStore = require('connect-mongo');
const mongoose = require('mongoose');
const helmet = require('helmet');
// import routes
const adsRoutes = require('./routes/ads.routes.js');
const authRoutes = require('./routes/auth.routes.js');
const app = express();
app.use(helmet.crossOriginResourcePolicy({ policy: "cross-origin" }))
const server = app.listen(process.env.PORT || 8000, () => {
console.log('Server is running on port: 8000');
});
// connect to DB
connectToDB()
// middleware for diferent ports client and server
if(process.env.NODE_ENV !== 'production') {
app.use(
cors({
origin: ['http://localhost:3000', 'http://localhost:8000'],
credentials: true,
})
);
}
app.use(express.urlencoded({ extended: false })); // x-www-form-urlencoded
app.use(express.json()); // form-data JSON format
app.use(session({
secret: process.env.DB_SECRET,
store: MongoStore.create(mongoose.connection),
resave: false,
saveUninitialized: false,
cookie: {
secure: process.env.NODE_ENV == 'production',
},
unset: 'destroy',
}));
// add routes
app.use('/api', adsRoutes); // add ads routes to server
app.use('/api/auth', authRoutes); // add auth routes to server
// Serve static files from the React app
app.use(express.static(path.join(__dirname, '/public')));
app.use(express.static(path.join(__dirname, '/client/build')));
app.use((req, res) => {
res.status(404).json({ message: '404 Not found...' });
});
module.exports = server;