-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (65 loc) · 2.11 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
70
71
72
73
74
75
76
77
78
79
80
const express = require("express")
const mongoose = require("mongoose");
const cors = require('cors');
const bodyParser = require("body-parser");
const dotenv = require("dotenv");
const path = require("path");
const { register } = require("module");
const app = express();
dotenv.config();
app.use(cors());
app.use(express.static(path.join(__dirname, 'pages')));
const port = process.env.PORT || 3000;
const username = process.env.MONGOODB_USERNAME;
const password = process.env.MONGOODB_PASSWORD;
mongoose.connect(`mongodb+srv://${username}:${password}@cluster0.5rpgsra.mongodb.net/registrationFormDB`, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const registrationSchema = new mongoose.Schema({
name: String,
email: String,
password: String
});
const Registration = mongoose.model("Regstraion", 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 {
// console.log(req.body);
const { name, email, password } = req.body;
// console.log(" Name ===>", name, "Email==>", email, "password==>", password);
// res.send({name,email,password});
const existingUser = await Registration.findOne({ email: email });
if (!existingUser) {
const registrationData = new Registration({
name,
email,
password
});
await registrationData.save();
console.log("Hello from backend");
res.send({status : 200});
}
else {
console.log("User already exist");
res.send({status : 404});
}
}
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}`);
});