feat: redirect if page is below 1 #1

Merged
snxraven merged 1 commits from Cyber/ravenscott-blog:main into main 2024-09-16 16:03:47 -04:00
Showing only changes of commit 9caedb3f87 - Show all commits

7
app.js
View File

@ -103,6 +103,11 @@ function getAllBlogPosts(page = 1, postsPerPage = 5) {
// Home Route (Blog Home with Pagination)
app.get('/', (req, res) => {
const page = parseInt(req.query.page) || 1;
if (page < 1) {
return res.redirect(req.hostname);
}
const postsPerPage = 5; // Set how many posts to display per page
const { blogPosts, totalPages } = getAllBlogPosts(page, postsPerPage);
@ -185,7 +190,7 @@ app.post('/contact', (req, res) => {
app.get('/blog/:slug', (req, res) => {
const slug = req.params.slug;
const markdownFile = fs.readdirSync(path.join(__dirname, 'markdown'))
.find(file => titleToSlug(file.replace('.md', '')) === slug);
.find(file => titleToSlug(file.replace('.md', '')) === slug);
if (markdownFile) {
const originalTitle = markdownFile.replace('.md', ''); // Original title with casing