-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
136 lines (122 loc) · 3.26 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
125
126
127
128
129
130
131
132
133
134
135
136
var express = require('express');
var app = express();
const bodyParser = require("body-parser");
var {mongoose} = require("./db/connect");
var {User} = require("./models/user");
var session = require('express-session');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(session({secret:'dasdnjnjfnsdnf3bn8wu3rjn2i3urn',resave:false,saveUninitialized:true}));
// var popup = require('popups');
app.set("view engine","ejs");
app.use(express.static("public"));
//DB
app.post('/insert', function(req, res) {
var item = {
"FName": req.body.fname,
"LName": req.body.lname,
"Email": req.body.email,
"Password": req.body.password,
"Mobile": req.body.mobile,
"City": req.body.department
};
var data = new User(item);
data.save((err, user)=>{
if(err)
{
console.log("LIGMA");
if(err.message.indexOf("11000") != -1)
{
// run some code here //
console.log("ERROR", "ID already taken");
res.render("signUp", {error: "Email Already exists!!"});
}
}
else
{
console.log("Tom");
res.redirect('/login');
}
});
});
//Routes
app.get("/",function(req,res){
res.render("login",{error:""});
})
app.post('/check', function(req, res) {
var entered_email = req.body.email;
var entered_password = req.body.password;
console.log(entered_email+":"+entered_password);
User.findOne({Email: entered_email}, function(err, user) {
// console.log("Before if!!");
if(err){ console.log(err);}
if (user==null) {
res.render("login", {error: "Invalid email or password!!"});
}
else {if(user.Password == entered_password){
console.log(user.Email+":"+user.Password);
req.session.user = user;
res.redirect("homepage");
}
else{
res.render("login", {error: "Invalid email or password!!"});
}
}
});
});
app.get("/login", function(req,res){
res.render("login",{error:" "});
});
app.get("/homepage", function(req,res){
if(!req.session.user){
return res.status(401).send();
}
res.render("index");
console.log('HELLO');
});
app.get('/logout', function(req, res) {
if (req.session) {
req.session.destroy();
return res.redirect('/');
}
});
app.get("/soups", function(req,res){
res.render("soups");
});
app.get("/rice", function(req,res){
res.render("rice");
});
app.get("/breads", function(req,res){
res.render("breads");
});
app.get("/smallbites", function(req,res){
res.render("smallbites");
});
app.get("/seafoods", function(req,res){
res.render("seafoods");
});
app.get("/desserts", function(req,res){
res.render("desserts");
});
app.get("/drinks", function(req,res){
res.render("drinks");
});
app.get("/icecream", function(req,res){
res.render("icecream");
});
// more
app.get("/starters", function(req,res){
res.render("starters");
});
app.get("/globalsalads", function(req,res){
res.render("globalsalads");
});
app.get("/curries", function(req,res){
res.render("curries");
});
app.get("/signup", function(req,res){
res.render("signUp", {error: ""});
});
// Connection for Port
app.listen(process.env.PORT,process.env.IP, function(){
console.log("Its running");
});