forked from FAForever/website
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor news-routes to be rendered in the backend + removed potentia…
…l memory-leaking setInterval
- Loading branch information
Showing
10 changed files
with
92 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
const fs = require('fs') | ||
|
||
function getNewsArticles() { | ||
const readFile = fs.readFileSync('./public/js/app/members/news.json', | ||
{encoding:'utf8', flag:'r'}); | ||
return JSON.parse(readFile); | ||
} | ||
|
||
function getNewsArticleBySlug(articles, slug) { | ||
let [newsArticle] = articles.filter((entry) => { | ||
if (entry.slug === slug) { | ||
return entry | ||
} | ||
}) ?? [] | ||
|
||
return newsArticle ?? null | ||
} | ||
|
||
function getNewsArticleByDeprecatedSlug(articles, slug) { | ||
let [newsArticle] = articles.filter((entry) => { | ||
if (entry.bcSlug === slug) { | ||
return entry | ||
} | ||
}) ?? [] | ||
|
||
return newsArticle ?? null | ||
} | ||
|
||
router.get(`/`, (req, res) => { | ||
res.render('news', {news: getNewsArticles()}); | ||
}) | ||
|
||
router.get(`/:slug`, (req, res) => { | ||
const newsArticles = getNewsArticles(); | ||
const newsArticle = getNewsArticleBySlug(newsArticles, req.params.slug) | ||
|
||
if (newsArticle === null) { | ||
const newsArticleByOldSlug = getNewsArticleByDeprecatedSlug(newsArticles, req.params.slug) | ||
|
||
if (newsArticleByOldSlug) { | ||
// old slug style, here for backward compatibility | ||
res.redirect(301, newsArticleByOldSlug.slug) | ||
|
||
return | ||
} | ||
|
||
res.redirect(req.baseUrl) | ||
|
||
return | ||
} | ||
|
||
res.render('newsArticle', { | ||
newsArticle: newsArticle | ||
}); | ||
|
||
}) | ||
|
||
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters