diff --git a/src/index.tsx b/src/index.tsx index 9e9f9e6..69c975b 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -3,9 +3,10 @@ import React from "react"; import ReactDOMServer from "react-dom/server"; import { Builder } from "xml2js"; import fs from "fs"; +import { Server } from "http"; import cryptoRandomString from "crypto-random-string"; -export const webServer = express() +const webApp = express() .use(express.static("static")) .use(express.urlencoded({ extended: true })) .get("/", (req, res) => @@ -27,14 +28,29 @@ export const webServer = express() ) ); - }) - .listen(8443); + }); -export const redirectServer = express() - .all("*", (req, res) => { - res.redirect(301, `https://www.kill-the-newsletter.com${req.originalUrl}`); - }) - .listen(8080); +export let developmentWebServer: Server; + +if (process.env.NODE_ENV === "production") { + const productionWebApp = express() + .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 = { name: string; diff --git a/src/test.ts b/src/test.ts index 55c5d86..2095ee5 100644 --- a/src/test.ts +++ b/src/test.ts @@ -1,9 +1,9 @@ -import { webServer, redirectServer, feedPath } from "."; +import { developmentWebServer, feedPath } from "."; import fetch from "node-fetch"; import fs from "fs"; -test("webServer", async () => { - const response = await fetch("http://localhost:8443", { +test("create feed", async () => { + const response = await fetch("http://localhost:8000", { method: "POST", body: new URLSearchParams({ name: "My Feed" }) }); @@ -14,17 +14,6 @@ test("webServer", async () => { 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(() => { - webServer.close(); - redirectServer.close(); + developmentWebServer.close(); });