-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
114 lines (100 loc) · 3.25 KB
/
index.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
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
104
105
106
107
108
109
110
111
112
113
114
const express = require('express')
const app = express()
const mongoose = require('mongoose')
// eslint-disable-next-line no-unused-vars
const dotenv = require('dotenv').config({ path: './config/.env' })
const bodyParser = require('body-parser')
const multer = require('multer')
const path = require('path')
// eslint-disable-next-line no-unused-vars
const bcrypt = require('bcrypt')
const cookieParser = require('cookie-parser')
const flash = require('connect-flash')
const morgan = require('morgan');
const session = require('express-session')
const sharp = require('sharp');
const Razorpay = require('razorpay');
app.use(session({
secret: 'heyboi',
cookie: {
maxAge: 6000000
}
}))
const fileStorage = multer.diskStorage({
// Destination to store image
destination: 'public/userImages',
filename: (req, file, cb) => {
cb(null, new Date().toISOString().replace(/:/g, '-') + file.originalname)
// file.fieldname is name of the field (image)
// path.extname get the uploaded file extension
}
})
const fileFilter = (req, file, cb) => {
if (file.mimetype === 'image/png' || file.mimetype === 'image/jpg' || file.mimetype === 'image/jpeg') {
// upload only png and jpg format
cb(null, true)
} else {
cb(null, false)
}
}
app.use(multer({ storage: fileStorage, fileFilter }).fields([{ name: 'image' }, { name: 'images', maxCount: 5 }, {name: 'bannerImage'}]))
app.use(flash())
app.use(cookieParser())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.set(express.static('public'))
// ---------------------------------------------
const PORT = process.env.PORT || 4000
mongoose.connect(process.env.DATABASE, { useNewUrlParser: true }, (err) => {
if (err) {
console.log('Database connection failed')
} else {
console.log('DB connected succefully')
}
})
app.set(express.static(path.join(__dirname, '/public')))
app.set('view engine', 'ejs')
app.set('views', path.join(__dirname, '/views'))
app.use(express.static('public'))
// app.use('/twilio-sms', twilioRouter)
const userRoute = require('./routes/userRoute')
const adminRoute = require('./routes/adminRoute')
const User = require('./models/userModel')
app.use('/', userRoute)
app.use('/admin', adminRoute)
app.use(function(req, res, next) {
const error = new Error(`Not found ${req.originalUrl}`)
if(req.originalUrl.startsWith('/admin')){
error.admin=true
}
error.status = 404
next(error)
});
// error handler
app.use(function(err, req, res, next) {
console.log(err,'hiiiiiiiiiiiiiiiiiiierrorkutan');
// render the error page
// res.status(err.status || 500);
if(err.status==404){
if(err.admin){
res.render('errorAdmin_404',{error:err.message});
}else{
console.log("hiiiiiiiii");
res.render('error_404',{error:err.message});
}
}else{
if(err.status==500){
res.render('error_500',{error:'unfinded error'})
}else{
if(err.admin){
console.log('yutytytyt');
res.render('errorAdmin_404',{error:'server down'})
}else{
res.render('error_404',{error:'server down'})
}
}
}
})
app.listen(PORT, () => {
console.log(`server started on http://localhost:${PORT}/`)
})