-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
66 lines (58 loc) · 1.87 KB
/
app.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
require('dotenv').config();
const express = require('express');
const app = express();
const cors = require('cors');
const mongoose = require('mongoose');
const { xss } = require('express-xss-sanitizer');
const cookieParser = require('cookie-parser');
const mongoSanitize = require('express-mongo-sanitize');
const userRouter = require('./router/userRouter');
const productRouter = require('./router/productRouter');
const errorHandler = require('./controller/errorController');
const helmet = require('helmet');
const path = require('path');
const compression = require('compression');
(async function () {
try {
await mongoose.connect(
`mongodb+srv://eloho:${process.env.MONGODB_PASSWORD}@ecommerce.7huagy2.mongodb.net/?retryWrites=true&w=majority`
);
console.log('database connected');
} catch (err) {
console.log('something went wrong');
}
})();
process.on('uncaughtExcepiton', (err) => {
console.log('UNCAUGHT EXCPTION');
console.log(err.name, err.message);
process.exit(1);
});
process.on('unhandledRejection', (err) => {
console.log('UNHANDLED REJECTION! 💥 Shutting down...');
console.log(err.name, err.message);
process.exit(1);
});
app.set('trust proxy', 1);
app.use(xss());
app.use(mongoSanitize());
app.use(compression());
app.use(helmet());
app.use('/uploads/:id', express.static(path.join(__dirname + 'public')));
app.use(
cors({
credentials: true,
origin: `${
process.env.NODE_ENV === 'development'
? 'http://localhost:5173'
: 'https://tech-freak.vercel.app'
}`,
methods: ['GET', 'POST', 'DELETE', 'PATCH'],
})
);
app.use(cookieParser());
app.use(express.json({ limit: '10kb' }));
app.use(express.urlencoded({ extended: true }));
app.use('/api/v1/users', userRouter);
app.use('/api/v1/products', productRouter);
app.use(errorHandler);
app.listen(process.env.PORT || 3000, () => console.log('server started'));