-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
42 lines (39 loc) · 1.37 KB
/
server.js
File metadata and controls
42 lines (39 loc) · 1.37 KB
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
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const port = 3000;
const app = express();
// MongoDB connection
mongoose.connect('mongodb+srv://vishalvardhan24816:QGfPLnD8XsVnTXis@cluster0.7hzg2mw.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0');
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
db.once('open', () => {
console.log('Connected to MongoDB');
});
const SubmissionSchema = new mongoose.Schema({
name: String,
email: String,
Age: Number,
phone: String
});
const Submission = mongoose.model('Submission', SubmissionSchema);
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static('public'));
app.post('/submit-form', async (req, res) => {
const { name, email, Age, phone } = req.body;
const newSubmission = new Submission({
name,
email,
Age,
phone
});
try {
await newSubmission.save();
res.status(200).json({ message: 'Form submitted successfully!' });
} catch (error) {
console.error('Error saving submission:', error);
res.status(500).json({ error: 'An error occurred while saving the submission.' });
}
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`));