-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
124 lines (103 loc) · 2.93 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
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
115
116
117
118
119
120
121
122
123
124
//Diclaration
const express = require('express');
const multer = require('multer');
const ejs = require('ejs');
const path = require('path');
var app=express();
var index=require('./controllers/index');
var cart=require('./controllers/cart');
var login=require('./controllers/login');
var user=require('./controllers/user');
var reg=require('./controllers/reg');
var adminlogin=require('./controllers/adminlogin');
var admindashboard=require('./controllers/admindashboard');
var error=require('./controllers/error');
var logout=require('./controllers/logout');
var user = require('./controllers/user');
var checkUser=require('./controllers/checkUser');
var checkout=require('./controllers/checkout');
var bodyParser=require('body-parser');
var expressSession=require('express-session');
var alertnode=require('alert-node');
var indexx = require.main.require('./models/index');
var port=1234;
//COnfigure
app.set('view engine','ejs');
//Middlewire
app.use(bodyParser.urlencoded({extended:false}));
app.use(expressSession({secret: 'My secret',resave: false,saveUninitialized: true}));
app.get('/',function(req,res){
res.redirect('/index');
});
// Static
app.use(express.static(path.join(__dirname, './Asset')));
app.all('*/*',function(req,res,next){
if(req.url=='/index' ||req.url=='/login' ||req.url=='/adminlogin' ||req.url=='/' ||req.url=='/reg' || req.url=='/advancesearch')
{
next();
return;
}
// if(req.session.loggedUser==null)
// {
// res.redirect('/login');
// }
if(req.session.loggedUser == null)
{
req.session.loggedUser="Guest";
res.send("Server Restarted. Refresh page to continue");
}
else
{
next();
}
});
// Set The Storage Engine
const storage = multer.diskStorage({
destination: './public/uploads/',
filename: function(req, file, cb){
cb(null,file.fieldname + '-' + Date.now() + path.extname(file.originalname));
}
});
// Init Upload
const upload = multer({
storage: storage,
limits:{fileSize: 1000000},
fileFilter: function(req, file, cb){
checkFileType(file, cb);
}
}).single('myImage');
// Check File Type
function checkFileType(file, cb){
// Allowed ext
const filetypes = /jpeg|jpg|png|gif/;
// Check ext
const extname = filetypes.test(path.extname(file.originalname).toLowerCase());
// Check mime
const mimetype = filetypes.test(file.mimetype);
if(mimetype && extname){
return cb(null,true);
} else {
cb('Error: Images Only!');
}
}
// EJS
app.set('view engine', 'ejs');
// Public Folder
app.use(express.static('./public'));
//Route
app.use('/index',index);
app.use('/cart',cart);
app.use('/login',login);
app.use('/user',user);
app.use('/reg',reg);
app.use('/adminlogin',adminlogin);
app.use('/admindashboard',admindashboard);
app.use('/error',error);
app.use('/logout',logout);
app.use('/checkUser',checkUser);
app.use('/checkout',checkout);
app.use('/user',user);
//Server setup
app.listen(port,function(){
console.log('Started port '+port);
});