This commit is contained in:
Leandro Facchinetti 2020-03-18 21:44:21 -04:00
parent 081a2929f7
commit 3187938a26
2 changed files with 28 additions and 23 deletions

View File

@ -3,9 +3,10 @@ import React from "react";
import ReactDOMServer from "react-dom/server"; import ReactDOMServer from "react-dom/server";
import { Builder } from "xml2js"; import { Builder } from "xml2js";
import fs from "fs"; import fs from "fs";
import { Server } from "http";
import cryptoRandomString from "crypto-random-string"; import cryptoRandomString from "crypto-random-string";
export const webServer = express() const webApp = express()
.use(express.static("static")) .use(express.static("static"))
.use(express.urlencoded({ extended: true })) .use(express.urlencoded({ extended: true }))
.get("/", (req, res) => .get("/", (req, res) =>
@ -27,14 +28,29 @@ export const webServer = express()
</Layout> </Layout>
) )
); );
}) });
.listen(8443);
export const redirectServer = express() export let developmentWebServer: Server;
.all("*", (req, res) => {
res.redirect(301, `https://www.kill-the-newsletter.com${req.originalUrl}`); if (process.env.NODE_ENV === "production") {
}) const productionWebApp = express()
.listen(8080); .use((req, res, next) => {
if (
req.protocol !== "https" ||
req.hostname !== "www.kill-the-newsletter.com"
)
return res.redirect(
301,
`https://www.kill-the-newsletter.com${req.originalUrl}`
);
next();
})
.use(webApp);
productionWebApp.listen(80);
productionWebApp.listen(443);
} else {
developmentWebServer = webApp.listen(8000);
}
type Inbox = { type Inbox = {
name: string; name: string;

View File

@ -1,9 +1,9 @@
import { webServer, redirectServer, feedPath } from "."; import { developmentWebServer, feedPath } from ".";
import fetch from "node-fetch"; import fetch from "node-fetch";
import fs from "fs"; import fs from "fs";
test("webServer", async () => { test("create feed", async () => {
const response = await fetch("http://localhost:8443", { const response = await fetch("http://localhost:8000", {
method: "POST", method: "POST",
body: new URLSearchParams({ name: "My Feed" }) body: new URLSearchParams({ name: "My Feed" })
}); });
@ -14,17 +14,6 @@ test("webServer", async () => {
expect(feed).toMatch("My Feed"); expect(feed).toMatch("My Feed");
}); });
test("redirectServer", async () => {
const response = await fetch("http://localhost:8080/something?other", {
redirect: "manual"
});
expect(response.status).toBe(301);
expect(response.headers.get("Location")).toBe(
"https://www.kill-the-newsletter.com/something?other"
);
});
afterAll(() => { afterAll(() => {
webServer.close(); developmentWebServer.close();
redirectServer.close();
}); });