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 { 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()
</Layout>
)
);
})
.listen(8443);
});
export const redirectServer = express()
.all("*", (req, res) => {
res.redirect(301, `https://www.kill-the-newsletter.com${req.originalUrl}`);
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();
})
.listen(8080);
.use(webApp);
productionWebApp.listen(80);
productionWebApp.listen(443);
} else {
developmentWebServer = webApp.listen(8000);
}
type Inbox = {
name: string;

View File

@ -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();
});