-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
103 lines (79 loc) · 2.53 KB
/
Copy pathapp.js
File metadata and controls
103 lines (79 loc) · 2.53 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const express = require('express');
const path = require('path');
const morgan = require('morgan');
const tourRouter = require('./routes/tourRoutes');
const userRouter = require('./routes/userRoutes');
const viewRouter = require('./routes/viewRoutes');
const reviewRouter = require('./routes/reviewRoutes');
const AppError = require('./utility/appError');
const ErrorController = require('./controllers/errorController');
const rateLimit = require('express-rate-limit');
const helmet = require('helmet');
const sanitize = require('express-mongo-sanitize');
const xss = require('xss-clean');
const hpp = require('hpp');
const cookieParser = require('cookie-parser');
const { urlencoded } = require('express');
const app = express();
app.set('view engine', 'pug');
app.set('views', path.join(__dirname, 'views'));
app.use(express.static(path.join(__dirname, 'public')));
app.use(helmet());
app.use(express.json({ limit: '10kb' }));
app.use(urlencoded({ limit: '10kb' }));
app.use(cookieParser());
app.use(sanitize());
app.use(xss());
app.use(hpp());
app.use(morgan('dev'));
app.use(express.static('public'));
app.use(express.static('dev-data'));
app.use((req, res, next) => {
console.log(req.cookies);
next();
});
app.use((req, res, next) => {
req.time = '12.04.2022';
next();
});
const limit = rateLimit({
max: 10,
windowMs: 1 * 60 * 60 * 1000,
message: 'Too many requests from this IP, Please try again later',
});
app.use('/api', limit);
// app.get('/', (req, res) => {
// res.status(200).json({
// message: 'This server is working!',
// data: 'Bu yerda json chiqishi kerak edi',
// });
// });
// app.get('/api/v1/tours', getAllTours);
// app.post('/api/v1/tours', addTour);
// app.get('/api/v1/tours/:id', getTourById);
// app.patch('/api/v1/tours/:id', updateTour);
// app.delete('/api/v1/tours/:id', deleteTour);
app.use('/', viewRouter);
app.use('/api/v1/tours', tourRouter);
app.use('/api/v1/users', userRouter);
app.use('/api/v1/reviews', reviewRouter);
app.all('*', function (req, res, next) {
next(new AppError(`this url has not found: ${req.originalUrl}`, 404));
});
app.use(ErrorController);
module.exports = app;
// Overview of Error handling
// 1)Operation errors
//
// 1. Xato url berish
// 2. Inputga kiritilayotgan xato malumot
// 3. Serverga tugri ulanolmaslik (Sekin internet)
// 4. Database ulanolmaslik
// 5....
// 2)Programming Errors
//
// 1. property ni uqiyotganda undefined bulishi
// 2. await dan foydalanish async siz
// 3. req.body ni ishlatish req.query urniga
// 4....
// Global Error handling middleware