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
let { submittedEmailAddresses } = await import("https://esm.town/v/klepra/submittedEmailAddresses");
export const renderFormAndSaveData = (req: express.Request, res: express.Response) => {
// A visit from a web browser? Serve a HTML page with a form
if (req.method === "GET") {
return res.send(`
<!DOCTYPE html>
<html>
<head>
<title>Email Form</title>
</head>
<body>
<!-- Change the action here to THIS val's Express endpoint! -->
<form action="https://vtdocs-renderFormAndSaveData.express.val.run" method="post">
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
`);
}
// Otherwise, someone has submitted a form so let's handle that
if (submittedEmailAddresses === undefined) {
submittedEmailAddresses = [];
}
const emailAddress = req.body.email;
if (submittedEmailAddresses.includes(emailAddress)) {
return res.send("you're already signed up!");
}
console.email(`${emailAddress} just signed up!`, "new sign up");
submittedEmailAddresses.push(emailAddress);
return res.send("thanks! you're signed up!");
};