-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
51 lines (43 loc) · 1.67 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
const express = require('express');
const path = require('path');
const cors = require('cors');
const session = require('express-session');
const corsOptions = require('./config/corsOptions');
const multer = require('multer')
const errorHandler = require('./middleware/errorHandler');
const {connectDb, sessionCollection} = require('./config/db');
const auth = require('./middleware/authentication');
const bodyParser = require('body-parser');
const PORT = process.env.PORT || 3500;
const app = express();
app.set("trust proxy", 1);
connectDb()
app.use(cors(corsOptions))
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use('/images', express.static(path.join(__dirname, '/images')));
app.use(
session({
secret: process.env.SESSION_SECRET || 'Il}/mav@hCn*CK!>""Zx=6?%p&oLgz<y',
resave: false,
saveUninitialized: false,
store: sessionCollection(),
expires: new Date(Date.now() + (7 * 24 * 60 * 60 * 1000)),
cookie: {
expires: new Date(Date.now() + (7 * 24 * 60 * 60 * 1000)),
sameSite: process.env.NODE_ENV === "production" ? 'none' : 'lax', // must be 'none' to enable cross-site delivery
secure: process.env.NODE_ENV === "production", // must be true if sameSite='none'
}
})
);
// ======================= Routes
app.use('/products', require('./routes/products'));
app.use('/orders', auth, require('./routes/orders'));
app.use('/carts', require('./routes/carts'));
app.use('/categories', require('./routes/categories'));
app.use('/password', require('./routes/users'));
app.use('/session', require('./routes/users'));
app.use(errorHandler);
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));