-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (55 loc) · 1.62 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
const express = require("express")
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const dotenv = require("dotenv");
const app = express ();
dotenv.config();
const port = process.env.PORT || 3000;
const username = process.env.MONGODB_USERNAME;
const password = process.env.MONGODB_PASSWORD;
mongoose.connect(`mongodb+srv://${username}:${password}@cluster0.meyfn4l.mongodb.net/Database`, {
serverSelectionTimeoutMS: 5000,
});
const registrationSchema = new mongoose.Schema({
name : String,
email : String,
password : String
});
const registration = mongoose.model("Registration", registrationSchema);
app.use(bodyParser.urlencoded ({ extended: true }));
app.use(bodyParser.json());
app.get("/", (req, res) => {
res.sendFile(__dirname + "/pages/index.html");
})
app.post("/register", async (req, res) => {
try{
const {name, email, password} = req.body;
const existingUser = await registration.findOne({email : email });
if(!existingUser) {
const registrationData = new registration({
name,
email,
password,
});
await registrationData.save();
res.redirect("/success");
}
else{
console.log("User alreadyexist");
res.redirect("/error");
}
}
catch (error) {
console.log(error);
res.redirect("error");
}
});
app.get("/success", (req, res)=>{
res.sendFile (__dirname+"/pages/success.html");
})
app.get("/error", (req, res)=>{
res.sendFile (__dirname+"/pages/error.html");
})
app.listen(port, ()=>{
console.log(`server is running on port ${port}`);
})