-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
75 lines (59 loc) · 2.01 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
67
68
69
70
71
72
73
74
75
const express = require('express');
const itemRoute = require('./routes/itemRoute');
const userRoute = require('./routes/userRoute');
const addressRoute = require('./routes/addressRoute');
const orderRoute = require('./routes/orderRoute');
const AppError = require('./utils/appError');
const viewRoute = require('./routes/viewRoute');
const cartRoute = require('./routes/cartRoute');
const webhook = require('./routes/webhook');
const orderController = require('./controllers/orderController')
const path = require('path');
const morgan = require('morgan');
const helmet = require('helmet');
const mongoSanitize = require('express-mongo-sanitize');
const xss = require('xss-clean');
const cookieParser = require('cookie-parser');
// const bodyParser = require('body-parser');
const app = express();
app.set('view engine', 'pug');
app.set('views', path.join(__dirname, 'views'))
// express.raw({type: 'application/json'});
// app.use(bodyParser.raw({type:'*/*'}));
// app.use(bodyParser.json());
//serving static file
app.use(express.json());
app.use(express.static(`${__dirname}/public`));
//helmet
app.use(helmet({
contentSecurityPolicy: false,
}));
//Development logging
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'));
}
app.use((req, res, next) => {
console.log('Hello from the middleware 👋');
next();
});
//body parser,reading data from body into req.body
app.use(express.json({ limit: '10kb' }));
app.use(express.urlencoded({ extended: true, limit: '10kb' }));
app.use(cookieParser());
app.use(mongoSanitize());
app.use(xss());
app.use((req, res, next) => {
req.requestTime = new Date().toISOString();
next();
});
app.use('/', viewRoute);
app.use('/api/v1/items', itemRoute);
app.use('/api/v1/users', userRoute);
app.use('/api/v1/address', addressRoute);
app.use('/api/v1/order', orderRoute);
app.use('/api/v1/cart', cartRoute);
app.use('/webhook', webhook);
app.all('*', (req, res, next) => {
next(new AppError(`Can't find ${req.originalUrl} on this server!`, 404));
});
module.exports = app;