-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
94 lines (81 loc) · 2.41 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
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const { User, FormData } = require('./src/Pages/UserAuthentication/mongo');
const app = express();
app.use(express.json());
app.use(cors());
app.use(bodyParser.json());
app.post('/', async (req, res) => {
const { email, password } = req.body;
try {
const user = await User.findOne({ email });
if (user) {
if (user.password === password) {
res.send('exist');
} else {
res.send('wrong details');
}
} else {
res.send('notexist');
}
} catch (error) {
console.error(error);
res.status(500).send('Internal server error');
}
});
app.post('/signup', async (req, res) => {
const { email, password } = req.body;
try {
const existingUser = await User.findOne({ email });
if (existingUser) {
res.send('exist');
} else {
const newUser = new User({ email, password });
await newUser.save();
res.send('notexist');
}
} catch (error) {
console.error(error);
res.status(500).send('Internal server error');
}
});
app.get('/admin/car/:model', async (req, res) => {
const { model } = req.params;
try {
const carData = await FormData.findOne({ carName: model });
if (carData) {
res.json(carData);
} else {
res.status(404).send('Car model not found');
}
} catch (err) {
console.error(err);
res.status(500).send('An error occurred while fetching the car data.');
}
});
app.post('/admin', async (req, res) => {
const { carName, price, carouselImages, colorImages } = req.body;
try {
const existingData = await FormData.findOne({ carName });
if (existingData) {
res.send('exist');
} else {
const formData = new FormData({
carName,
price,
carouselImages,
colorImages
});
await formData.save();
res.send('Form data saved successfully!');
}
} catch (err) {
console.error(err);
res.status(500).send('An error occurred while saving the form data.');
}
});
const PORT = 8000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});