Skip to content

Commit 8fce9a7

Browse files
feat: adds Jason's express web server (#576)
* lesson_24 * lesson 24 * fix: moves files into proper arrangement Signed-off-by: Anthony D. Mays <[email protected]> * feat: gets form submissions working Signed-off-by: Anthony D. Mays <[email protected]> --------- Signed-off-by: Anthony D. Mays <[email protected]> Co-authored-by: Anthony D. Mays <[email protected]>
1 parent 7fb9330 commit 8fce9a7

File tree

9 files changed

+1882
-0
lines changed

9 files changed

+1882
-0
lines changed

lesson_24/webserver_jwatson/index.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const express = require("express");
2+
const app = express();
3+
const port = 3000;
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 'RMillerCDclone' directory
12+
app.use(express.static(path.join(__dirname, 'public')));
13+
14+
// Handle form submission
15+
app.post("/submit", (req, res) => {
16+
const { firstname, lastname, email, message } = req.body;
17+
18+
// Log the form data to the console
19+
console.log(`First Name: ${firstname}`);
20+
console.log(`Last Name: ${lastname}`);
21+
console.log(`Email: ${email}`);
22+
console.log(`Message: ${message}`);
23+
24+
// Send a response back to the user
25+
res.send(`
26+
<h1>Thank you for your submission, ${firstname}!</h1>
27+
<p>We have received your message and will get back to you at ${email} soon.</p>
28+
<a href="/">Go back to the contact form</a>
29+
`);
30+
});
31+
32+
// Start the server
33+
app.listen(port, () => {
34+
console.log(`Listening at http://localhost:${port}`);
35+
});

0 commit comments

Comments
 (0)