-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (58 loc) · 1.84 KB
/
index.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
import express from 'express';
const app = express();
const PORT = process.env.PORT || 4000;
import mongoose from 'mongoose';
import session from 'express-session';
import cors from 'cors';
import MongoStore from 'connect-mongo';
import { errorHandler, logger, checkAuthorized } from './utils/middleware.js';
import userRouter from './routes/users.js';
import productRouter from './routes/products.js';
import cartRouter from './routes/carts.js';
import orderRouter from './routes/order.js';
mongoose
.connect(process.env.MONGODBURI)
.then(() => console.log('Connected to Database'))
.catch((err) => console.log(err));
const URL =
process.env.NODE_ENV === 'production'
? process.env.PRODURL
: process.env.DEVURL;
app.use(
cors({
origin: URL,
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH'],
allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With'],
})
);
app.use(express.json());
app.use(logger);
app.set('trust proxy', 1);
app.use(
session({
saveUninitialized: false,
resave: false,
secret: process.env.SESSION_SECRET,
cookie: {
secure: process.env.NODE_ENV === 'production',
sameSite: process.env.NODE_ENV === 'production' ? 'none' : 'lax',
maxAge: 60000 * 60 * 24,
},
store: MongoStore.create({
client: mongoose.connection.getClient(),
}),
})
);
app.get('/', (req, res) => {
res.status(200).send('Server running');
});
app.use('/api/user', userRouter);
app.use('/api/products', checkAuthorized, productRouter);
app.use('/api/cart', checkAuthorized, cartRouter);
app.use('/api/order', checkAuthorized, orderRouter);
app.use(errorHandler);
app.listen(PORT, () => {
console.log(`Server started on ${URL}:${PORT}`);
});
export default app;