-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
87 lines (77 loc) · 1.81 KB
/
app.js
File metadata and controls
87 lines (77 loc) · 1.81 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
var express=require("express"),
bodyparser=require("body-parser"),
flash=require("connect-flash");
var app=express();
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/ieee_web', {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => console.log('Connected to DB!'))
.catch(error => console.log(error.message));
app.use(flash());
app.use(require("express-session")({
secret:"Hey this is secret Shhhhhhh !!!",
resave:false,
saveUninitialized:false
}))
app.use(bodyparser.urlencoded({extended:true}));
app.set("view engine","ejs");
app.use(express.static("public"));
app.use(function(req,res,next){
res.locals.currentuser=req.user;
res.locals.error=req.flash("error");
res.locals.warn=req.flash("warn");
res.locals.message=req.flash("success");
next();
})
var userschema= new mongoose.Schema({
fname : String,
lname: String,
email : String,
uname: String,
pname:String
});
var user=mongoose.model("user",userschema);
//user.create({
// fname : "mayank",
// lname: "kushwah",
// email : "sess@gmail.com",
// uname:"makarov",
// pname:"123321"
//});
app.get("/",function(req,res){
res.render("home");
});
//app.get("/form",function(req,res){
// user.find({},function(err,user){
// if(err){
// console.log("error");
// }else{
// res.render("form",{user:user});
// }
// });
//});
app.get("/form",function(req,res){
user.find({},function(err,user){
if(err){
console.log("error")
}else{
res.render("form",{user:user})
}
})
})
app.post("/form",function(req,res){
user.create(req.body.user,function(err,newuser){
if(err){
console.log("some error");
}else{
req.flash("error","Please Log In First !!");
res.render("welcome");
}
})
});
app.listen(process.env.PORT || 4000, process.env.IP ,function()
{
console.log("listening to port 4000");
});