-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·60 lines (47 loc) · 1.5 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
const express = require('express');
const dotenv = require('dotenv');
const cookieParser = require('cookie-parser');
const morgan = require('morgan');
const cors = require('cors');
const mongoSanitize = require('express-mongo-sanitize');
dotenv.config({ path: './config/config.env' });
require('colors');
const app = express();
const connectDB = require('./config/db');
//Import Custom middleware
const errorHandler = require('./middleware/error');
//Import routes
const auth = require('./routes/auth');
const categories = require('./routes/categories');
const products = require('./routes/products');
const carts = require('./routes/carts');
// Mount connection
connectDB();
const PORT = process.env.PORT || 7000;
//Mount cookieParser
app.use(cookieParser());
// Enable cors
app.use(cors());
// Enable mongoSanitize
app.use(mongoSanitize());
//Mount json parser
app.use(express.json());
if (process.env.NODE_ENV === 'development') app.use(morgan('dev'));
//Mount routes
app.use('/api/v1/auth', auth);
app.use('/api/v1/categories', categories);
app.use('/api/v1/products', products);
app.use('/api/v1/carts', carts);
//Mount erroHandler
app.use(errorHandler);
const server = app.listen(
PORT,
console.log(
`Server running in ${process.env.NODE_ENV} mode on port ${process.env.PORT}`
.yellow.bold
)
);
process.on('unhandledRejection', (err, promise) => {
console.log(`Error : ${err.message}`.red);
server.close(() => process.exit(1));
});