express server and form

Ana Luisa Costa 1 year ago
commit 3258da75f67657600bcf56fb43632f8b563e90da
3 changed files with 47 additions and 0 deletions
+11-0package.json ADDED
View File
@@ -0,0 +1,11 @@
1+{
2+ "name": "contact-form",
3+ "version": "1.0.0",
4+ "main": "server.js",
5+ "scripts": {"start": "node server.js"},
6+ "dependencies": {
7+ "dotenv": "^16.4.5",
8+ "express": "^4.21.0",
9+ "nodemailer": "^6.9.15"
10+ }
11+}
+11-0public/index.html ADDED
View File
@@ -0,0 +1,11 @@
1+<!doctype html>
2+<html lang="en">
3+<head><meta charset="utf-8"><title>Contact</title></head>
4+<body>
5+ <form action="/send" method="post">
6+ <input name="name" placeholder="Your name" required>
7+ <textarea name="message" required></textarea>
8+ <button>Send</button>
9+ </form>
10+</body>
11+</html>
+25-0server.js ADDED
View File
@@ -0,0 +1,25 @@
1+require("dotenv").config();
2+const express = require("express");
3+const nodemailer = require("nodemailer");
4+
5+const app = express();
6+app.use(express.urlencoded({ extended: false }));
7+app.use(express.static("public"));
8+
9+const mail = nodemailer.createTransport({
10+ host: process.env.SMTP_HOST,
11+ port: 587,
12+ auth: { user: process.env.SMTP_USER, pass: process.env.SMTP_PASS },
13+});
14+
15+app.post("/send", async (req, res) => {
16+ await mail.sendMail({
17+ from: process.env.SMTP_USER,
18+ to: process.env.SMTP_USER,
19+ subject: `Contact form: ${req.body.name}`,
20+ text: req.body.message,
21+ });
22+ res.send("Thanks! We will get back to you.");
23+});
24+
25+app.listen(3000, () => console.log("http://localhost:3000"));