forked from anuragverma108/SwapReads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
148 lines (125 loc) · 4.53 KB
/
server.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
137
138
139
140
141
142
143
144
145
146
147
148
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const nodemailer = require("nodemailer");
const RegisterSchema = require("./assets/validation/zodschema");
const validate = require("./assets/validation/validate.schema");
const cors=require("cors")
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true })); // For parsing application/x-www-form-urlencoded
app.use(cors());
const MONGO_URI = "mongodb+srv://nishantkaushal0708:[email protected]/";
const dbConnect = async () => {
try {
await mongoose.connect(MONGO_URI, {
serverSelectionTimeoutMS: 30000, // 30 seconds timeout
});
console.log("DB connected");
} catch (err) {
console.log("DB failed", err);
}
};
dbConnect().then(() => {
const User = mongoose.model("User", { username: String, password: String });
app.post("/signup", validate(RegisterSchema), async (req, res) => {
const { username, password } = req.body;
const userExists = await User.findOne({ username });
if (userExists) {
res.json({ success: false, message: "Username already exists." });
} else {
const newUser = new User({ username, password });
await newUser.save();
res.json({ success: true });
}
});
app.post("/login", validate(RegisterSchema), async (req, res) => {
const { username, password } = req.body;
const user = await User.findOne({ username, password });
if (user) {
res.json({ success: true });
} else {
res.json({ success: false, message: "Invalid username or password." });
}
});
// Book Exchange/Selling
const bookSchema = new mongoose.Schema({
title: String,
author: String,
price: Number,
sellerEmail: String,
});
const Book = mongoose.model("Book", bookSchema);
app.post("/sellBook", async (req, res) => {
const { title, author, price, sellerEmail } = req.body;
const newBook = new Book({ title, author, price, sellerEmail });
newBook.save()
.then((book) => {
sendListingEmailToSeller(sellerEmail, book.title);
res.json({ success: true, message: "Book listing added successfully!" });
})
.catch((err) => {
console.log(err);
res.json({ success: false, message: "Internal Server Error" });
});
});
app.post("/buyBook", async (req, res) => {
const { bookID, buyerEmail } = req.body;
Book.findById(bookID)
.then((book) => {
if (!book) {
return res.json({ success: false, message: "Book Not Found." });
}
sendBuyingEmailToSeller(book.sellerEmail, book.title, book.price, book.author, buyerEmail);
res.json({ success: true, message: "Email Sent to Seller" });
})
.catch((err) => {
console.log(err);
res.json({ success: false, message: "Internal Server Error" });
});
});
// Contact form endpoint
app.post("/contact", async (req, res) => {
const { name, email, subject, message } = req.body;
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: "enter_you_mail",
pass: "Enter_YOUR_APP_PASSWORD",
},
});
const mailOptions = {
from: email,
to: "enter_you_mail",
subject: `You Have New Query from ${name} Regarding ${subject}`,
html: `Hey there is query from is ${name} Email: ${email} Message:<p style="color: red;"> ${message}</p>`,
};
const acknowledgmentOptions = {
from: "enter_you_mail",
to: email,
subject: "Acknowledgment of your message",
html: `<div style="font-family: Arial, sans-serif; line-height: 1.6;">
<p>Dear <strong>${name}</strong>,</p>
<p>Thank you for reaching out to us. We have received your message and will get back to you shortly.</p>
<p style="margin-top: 20px;">Best regards,<br><strong>SwapReads.com</strong></p>
</div>`,
};
try {
await transporter.sendMail(mailOptions);
await transporter.sendMail(acknowledgmentOptions);
res.json({ success: true, message: "Message sent successfully" });
} catch (err) {
console.error("Error sending email:", err);
res.json({ success: false, message: "Error sending message" });
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
});
app.post('/subscribe',(req,res)=>{
let email = req.body;
console.log(email);
})
app.listen(3000, () => console.log("Server is running on port 3000"));