-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
61 lines (52 loc) · 1.66 KB
/
Copy pathapp.js
File metadata and controls
61 lines (52 loc) · 1.66 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const express = require('express');
const path = require('path');
const connectDB = require('./mongodbVSCodePlaygroundDB');
const app = express();
const PORT = 4000;
// Connect to MongoDB
connectDB();
// Middleware to parse incoming requests with JSON payloads
app.use(express.json());
// Serve static files
app.use(express.static(path.join(__dirname, 'public')));
// Root route
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// Example route to save data to MongoDB
app.post('/api/data', async (req, res) => {
try {
const Data = mongoose.model('Data', new mongoose.Schema({ name: String }));
const newData = new Data({ name: req.body.name });
await newData.save();
res.status(201).send('Data saved successfully');
} catch (error) {
console.error(error);
res.status(500).send('Server Error');
}
});
// Start server
app.listen(PORT, () => {
console.log(Server running at http://localhost:${PORT});
});
document.getElementById('data-form').addEventListener('submit', async (e) => {
e.preventDefault();
const name = document.getElementById('name').value;
try {
const response = await fetch('/api/data', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ name }),
});
if (response.ok) {
M.toast({ html: 'Data saved successfully!' });
} else {
M.toast({ html: 'Failed to save data!' });
}
} catch (error) {
console.error('Error:', error);
M.toast({ html: 'Server Error!' });
}
});