-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
81 lines (67 loc) · 2.58 KB
/
server.js
File metadata and controls
81 lines (67 loc) · 2.58 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const axios = require('axios');
const fs = require('fs');
const app = express();
const port = 3000;
// Middleware
app.use('/', express.static(path.join(__dirname, 'default')));
app.use(express.static(__dirname));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Routes for each section
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'default/index.html'));
});
app.get('/home', (req, res) => {
res.sendFile(path.join(__dirname, 'home/index.html'));
});
app.get('/skills', (req, res) => {
res.sendFile(path.join(__dirname, 'skills/index.html'));
});
app.get('/career', (req, res) => {
res.sendFile(path.join(__dirname, 'career/index.html'));
});
app.get('/projects', (req, res) => {
res.sendFile(path.join(__dirname, 'projects/index.html'));
});
app.get('/contact', (req, res) => {
res.sendFile(path.join(__dirname, 'contact/index.html'));
});
// API endpoint for contact form
app.post('/api/contact', async (req, res) => {
try {
// Get webhook URL from webhook.json
const webhookData = JSON.parse(fs.readFileSync('webhook.json', 'utf8'));
const webhookUrl = webhookData.discord;
const { name, email, message } = req.body;
// Validate input
if (!name || !email || !message) {
return res.status(400).json({ success: false, message: 'All fields are required' });
}
// Format message for Discord
const discordMessage = {
embeds: [{
title: 'New Contact Form Submission',
color: 0x3b82f6, // Blue color
fields: [
{ name: 'Name', value: name, inline: true },
{ name: 'Email', value: email, inline: true },
{ name: 'Message', value: message },
{ name: 'Timestamp', value: new Date().toLocaleString('en-US', { timeZone: 'America/New_York' }) }
]
}]
};
// Send to Discord webhook
await axios.post(webhookUrl, discordMessage);
res.status(200).json({ success: true, message: 'Message sent successfully!' });
} catch (error) {
console.error('Error sending message:', error);
res.status(500).json({ success: false, message: 'Failed to send message. Please try again later.' });
}
});
// Start the server
app.listen(port, () => {
console.log(`Portfolio website running at http://localhost:${port}`);
});