-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
50 lines (43 loc) · 1.17 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
import express from 'express';
import mongoose from 'mongoose';
import bodyParser from 'body-parser';
import cors from 'cors';
const app = express();
const port = 5000;
// Middleware
app.use(bodyParser.json());
app.use(cors({
origin: 'http://localhost:5173', // Adjust this to match your frontend URL
optionsSuccessStatus: 200
}));
// Connect to MongoDB
mongoose.connect('mongodb://localhost:3301/riseUP')
.then(() => console.log('Connected to MongoDB'))
.catch((err) => {
console.error('MongoDB connection error:', err);
process.exit(1);
});
// Define a schema
const formSchema = new mongoose.Schema({
firstName: String,
lastName: String,
email: String,
message: String,
});
// Create a model
const Form = mongoose.model('Form', formSchema);
// Define routes
app.post('/submit', async (req, res) => {
const formData = new Form(req.body);
try {
await formData.save();
res.status(201).send('Form data saved');
} catch (error) {
console.error('Error saving form data:', error);
res.status(400).send('Error saving form data');
}
});
// Start the server
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});