|
| 1 | +const express = require("express"); |
| 2 | +const app = express(); |
| 3 | +const port = 5000; |
| 4 | +const cors = require("cors"); |
| 5 | +const path = require("path"); |
| 6 | + |
| 7 | +app.use(express.urlencoded({ extended: true })); |
| 8 | +app.use(express.json()); |
| 9 | +app.use(cors()); // Enable CORS globally |
| 10 | + |
| 11 | +// Serve static files from the 'express_app' directory |
| 12 | +app.use(express.static(path.join(__dirname, 'express_app'))); |
| 13 | + |
| 14 | +// Serve the contact form at the root |
| 15 | +app.get("/", (req, res) => { |
| 16 | + res.sendFile(path.join(__dirname, 'contact.html')); |
| 17 | +}); |
| 18 | + |
| 19 | +// Handle form submission |
| 20 | +app.post("/submit", (req, res) => { |
| 21 | + const { name, email, message } = req.body; |
| 22 | + |
| 23 | + // Log the form data to the console |
| 24 | + console.log("Received submission:"); |
| 25 | + console.log(`Name: ${name}`); |
| 26 | + console.log(`Email: ${email}`); |
| 27 | + console.log(`Message: ${message}`); |
| 28 | + |
| 29 | + // Send a response back to the user |
| 30 | + res.send(` |
| 31 | + <h1>Thank you for your submission, ${name}!</h1> |
| 32 | + <p>We have received your message and will get back to you at ${email} soon.</p> |
| 33 | + <a href="/">Go back to the contact form</a> |
| 34 | + `); |
| 35 | +}); |
| 36 | + |
| 37 | +// Start the server |
| 38 | +app.listen(port, () => { |
| 39 | + console.log(`Listening at http://localhost:${port}`); |
| 40 | +}); |
0 commit comments