-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
57 lines (44 loc) · 1.83 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
51
52
53
54
55
56
57
// Import necessary modules
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser'); // For parsing form data
const app = express();
const port = 3000;
// Middleware to parse URL-encoded data
app.use(bodyParser.urlencoded({ extended: true }));
// Serve static files from the "public" directory
app.use(express.static(path.join(__dirname, 'public')));
// Serve specific HTML files
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'html', 'home.html'));
});
// (Other routes)
// Profile Update Route
app.post('/update-profile', (req, res) => {
const { name, email, phone, address } = req.body;
// Here you would typically update the user's profile in the database.
// For demonstration, we'll just log the details.
console.log('Updated Profile:', { name, email, phone, address });
// Redirect to profile page or render a success message
res.redirect('/profile'); // Assumes you have a route for '/profile'
});
// Handle 404 errors
app.use((req, res) => {
res.status(404).send('Page Not Found');
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
const mongoose = require('mongoose');
const User = require('./models/User'); // Import your user model
app.post('/update-profile', async (req, res) => {
const { name, email, phone, address } = req.body;
const userId = req.session.userId; // Assuming you store the user ID in session
try {
await User.findByIdAndUpdate(userId, { name, email, phone, address });
res.redirect('/profile'); // Redirect to profile page after updating
} catch (error) {
console.error('Error updating profile:', error);
res.status(500).send('Failed to update profile');
}
});