-
Notifications
You must be signed in to change notification settings - Fork 16
/
server.js
78 lines (64 loc) · 1.9 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// load env-vars
require('dotenv').config();
// requiring dependencies
const express = require('express');
const cors = require('cors');
const cookieParser = require('cookie-parser');
// initialize express
const app = express();
// requiring routers
const paymentRouter = require('./routes/paymentRouter');
const productRouter = require('./routes/productRouter');
const adminRouter = require('./routes/adminRouter');
const orderRouter = require('./routes/orderRouter');
const uploadRouter = require('./routes/uploadRouter');
// requiring middlewares
const errorMiddleware = require('./middleware/Error');
// require db configs
const connectToDb = require('./config/db');
// require cloudinary configs
const cloudinary = require('./config/cloudinary');
// uncaught exception
process.on('uncaughtException', (err) => {
console.log(`Error: ${err.message}`);
console.log(`Server shutting down due to uncaught exception`);
process.exit(1);
});
// connect to db
connectToDb();
// using middlewares
app.use(
cors({
origin: [/netlify\.app$/, /localhost:\d{4}$/],
credentials: true,
})
);
app.use(express.json({ limit: '20mb' }));
app.use(cookieParser());
// basic api route
app.get('/', (req, res) => {
res.status(200).json({
success: true,
message: 'API service running 🚀',
});
});
// using routers
app.use('/api/payment', paymentRouter);
app.use('/api/products', productRouter);
app.use('/api/admin', adminRouter);
app.use('/api/orders', orderRouter);
app.use('/api/upload', uploadRouter);
// using other middlewares
app.use(errorMiddleware);
// starting server
const server = app.listen(process.env.PORT || 5000, () => {
console.log('Server running');
});
// unhandled promise rejection
process.on('unhandledRejection', (err) => {
console.log(`Error: ${err.message}`);
console.log(`Server shutting down due to unhandled promise rejection`);
server.close(() => {
process.exit(1);
});
});