This commit is contained in:
Leandro Facchinetti 2020-03-19 11:50:15 -04:00
parent f192ef56ca
commit f22e678b31
1 changed files with 32 additions and 33 deletions

View File

@ -7,22 +7,10 @@ import xml2js from "xml2js";
import fs from "fs"; import fs from "fs";
import cryptoRandomString from "crypto-random-string"; import cryptoRandomString from "crypto-random-string";
const webApp = express(); const webApp = express()
if (process.env.NODE_ENV === "production") .use(express.static("static"))
webApp.use((req, res, next) => { .use(express.urlencoded({ extended: true }))
if ( .get("/", (req, res) =>
req.protocol !== "https" ||
req.hostname !== "www.kill-the-newsletter.com"
)
return res.redirect(
301,
`https://www.kill-the-newsletter.com${req.originalUrl}`
);
next();
});
webApp.use(express.static("static"));
webApp.use(express.urlencoded({ extended: true }));
webApp.get("/", (req, res) =>
res.send( res.send(
renderHTML( renderHTML(
<Layout> <Layout>
@ -30,8 +18,8 @@ webApp.get("/", (req, res) =>
</Layout> </Layout>
) )
) )
); )
webApp.post("/", (req, res) => { .post("/", (req, res) => {
const inbox: Inbox = { name: req.body.name, token: newToken() }; const inbox: Inbox = { name: req.body.name, token: newToken() };
fs.writeFileSync(feedPath(inbox.token), renderXML(Feed(inbox))); fs.writeFileSync(feedPath(inbox.token), renderXML(Feed(inbox)));
res.send( res.send(
@ -41,7 +29,7 @@ webApp.post("/", (req, res) => {
</Layout> </Layout>
) )
); );
}); });
const emailApp = new SMTPServer({ const emailApp = new SMTPServer({
authOptional: true, authOptional: true,
@ -81,6 +69,17 @@ export const webServer = webApp.listen(
process.env.NODE_ENV === "production" ? 80 : 8000 process.env.NODE_ENV === "production" ? 80 : 8000
); );
if (process.env.NODE_ENV === "production") { if (process.env.NODE_ENV === "production") {
webApp.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();
});
webApp.listen(443); webApp.listen(443);
} }
export const emailServer = emailApp.listen( export const emailServer = emailApp.listen(