-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
104 lines (77 loc) · 2.04 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
const express =require("express");
const app = express();
const mongoose = require("mongoose");
const port = 3001;
const bodyParser = require("body-parser");
// DB CONNECTION
// mongoose.connect("mongodb://localhost:27017/login", {
// useNewUrlParser : true,
// useUnifiedTopology:true
// }, (err) =>{
// if(!err)
// {
// console.log("connected to db ")
// }else {
// console.log("error")
// }
// })
app.use(express());
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
const connectionUrl = 'mongodb+srv://doadmin:079625yqT83eXMWV@db-mongodb-blr1-37098-c28f0982.mongo.ondigitalocean.com/admin?tls=true&authSource=admin';
app.get('/',(req,res)=>{
res.send("Hello World")
})
app.put('/login',(req,res)=>{
console.log(req.body.username);
if (req.body.username==="mj"&req.body.password==="Pace2012#") {
return res.json({status: 200, message: "Login Successfully! Welcome mj"});
} else {
return res.json({
status: 401,
message: "Wrong User Name Or Password"});
}
// res.send("Hello World")
})
// mongoose.connect(connectionUrl,{
// useNewUrlParser: true,
// useUnifiedTopology: true,
// }).then(su=>{
// if (su) {
// console.log('Mongodb Connect')
// }
// }).catch(e=>{
// console.log(e)
// })
mongoose.connect("mongodb://localhost:27017/login", {
useNewUrlParser : true,
useUnifiedTopology:true
}, (err) =>{
if(!err)
{
console.log("connected to db ")
}else {
console.log("error")
}
})
// SCHEMA
const sch = {
name : String,
email : String,
id : Number
}
const monmodel = mongoose.model("logindata",sch);
// Post
app.post("/post",async(req,res) => {
console.log("inside post function");
const data = new monmodel ({
name:req.body.name,
email:req.body.email,
id:req.body.id
});
const val = await data.save();
res.json(val);
})
app.listen(port, ()=>{
console.log("The Server is running at" + port);
})