kill-the-newsletter/src/server.tsx

44 lines
910 B
TypeScript
Raw Normal View History

2020-03-18 04:07:48 +01:00
import express from "express";
import React from "react";
import ReactDOMServer from "react-dom/server";
2020-03-18 17:22:18 +01:00
import { Layout, Form, Created } from "./components";
import fs from "fs";
import cryptoRandomString from "crypto-random-string";
2020-03-18 04:07:48 +01:00
const app = express();
app.use(express.static("static"));
2020-03-18 17:22:18 +01:00
app.use(express.urlencoded());
2020-03-18 04:07:48 +01:00
app.get("/", (req, res) =>
2020-03-18 05:49:41 +01:00
res.send(
2020-03-18 16:12:46 +01:00
render(
<Layout>
<Form></Form>
</Layout>
)
2020-03-18 05:49:41 +01:00
)
2020-03-18 04:07:48 +01:00
);
2020-03-18 17:22:18 +01:00
app.post("/", (req, res) => {
res.send(
render(
<Layout>
<Created
name={req.body.name}
token={cryptoRandomString({
length: 20,
characters: "1234567890qwertyuiopasdfghjklzxcvbnm"
})}
></Created>
</Layout>
)
);
});
2020-03-18 16:12:46 +01:00
app.listen(8000);
function render(component: React.ReactElement): string {
return `<!DOCTYPE html>\n${ReactDOMServer.renderToStaticMarkup(component)}`;
}