-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.js
More file actions
40 lines (33 loc) · 1.21 KB
/
node.js
File metadata and controls
40 lines (33 loc) · 1.21 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
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const path = require('path');
const app = express();
app.use(cors());
app.use(express.json());
// Serve static files (HTML, CSS, JS, images, etc.)
app.use(express.static(__dirname));
// Connect to MongoDB
mongoose.connect('mongodb://localhost:27017/joinform', {
useNewUrlParser: true,
useUnifiedTopology: true
});
// Flexible schema
const employeeSchema = new mongoose.Schema({}, { strict: false });
const Employee = mongoose.model('Employee', employeeSchema);
// Save form data
app.post('/api/employees', async (req, res) => {
try {
const employee = new Employee(req.body);
await employee.save();
res.status(201).json(employee);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// Optional: Route for each HTML file (not strictly needed if using express.static)
// app.get('/', (req, res) => res.sendFile(path.join(__dirname, 'new.html')));
// app.get('/nomination.html', (req, res) => res.sendFile(path.join(__dirname, 'nomination.html')));
// ...repeat for other HTML files if you want direct routes
// Start server
app.listen(8080, () => console.log('Server running on port 8080'));