-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (63 loc) · 2.15 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
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
require('dotenv').config();
const app = express();
// Connect to MongoDB
const connectDB = async () => {
try {
await mongoose.connect(process.env.MONGO_URI);
console.log('Database connected');
} catch (error) {
console.error('Database connection error:', error);
process.exit(1);
}
};
// Call the function to ensure database connection is established
connectDB().then(() => {
// Start the server inside the then() to ensure it starts after the database connection is established
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
});
const formSchema = new mongoose.Schema({
name: { type: String, required: true },
email: { type: String, required: true, unique: true }, // Ensure email is unique
phone: { type: String, required: true, unique: true }, // Ensure phone is unique
babyStage: { type: String, required: true },
});
// Apply the unique validator plugin to formSchema
formSchema.plugin(require('mongoose-unique-validator'));
const Form = mongoose.model('Form', formSchema);
// Middleware
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Endpoint to handle form submission
app.post('/book-demo', async (req, res) => {
const { name, email, countryCode, phone, babyStage } = req.body;
// Concatenate countryCode and phone
const fullPhone = `${countryCode}' '${phone}`;
const formData = new Form({
name,
email,
phone: fullPhone, // Used the concatenated phone number here
babyStage,
});
try {
await formData.save();
res.status(201).send({ message: 'Form data saved successfully' });
} catch (error) {
console.error('Error saving form data:', error);
res.status(500).send({ message: 'Error saving form data' });
}
});
// Serve the HTML file
app.get('/', (req, res) => {
res.sendFile(__dirname + '/demo-form.html');
});
// Start the server
// const PORT = process.env.PORT || 3000;
// app.listen(PORT, () => {
// console.log(`Server is running on port ${PORT}`);
// });