Add error message when no posts are found
This commit is contained in:
parent
9ec9374ea2
commit
ec891ab070
12
app.js
12
app.js
@ -68,6 +68,10 @@ function getAllBlogPosts(page = 1, postsPerPage = 5, searchQuery = '') {
|
|||||||
blogFiles = blogFiles.filter(file => file.toLowerCase().includes(lowerCaseQuery));
|
blogFiles = blogFiles.filter(file => file.toLowerCase().includes(lowerCaseQuery));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (blogFiles.length === 0) {
|
||||||
|
return { blogPosts: [], totalPages: 0 }; // Return empty results if no files
|
||||||
|
}
|
||||||
|
|
||||||
blogFiles.sort((a, b) => {
|
blogFiles.sort((a, b) => {
|
||||||
const statA = fs.statSync(path.join(__dirname, 'markdown', a)).birthtime;
|
const statA = fs.statSync(path.join(__dirname, 'markdown', a)).birthtime;
|
||||||
const statB = fs.statSync(path.join(__dirname, 'markdown', b)).birthtime;
|
const statB = fs.statSync(path.join(__dirname, 'markdown', b)).birthtime;
|
||||||
@ -105,15 +109,19 @@ app.get('/', (req, res) => {
|
|||||||
const postsPerPage = 5;
|
const postsPerPage = 5;
|
||||||
const { blogPosts, totalPages } = getAllBlogPosts(page, postsPerPage, searchQuery);
|
const { blogPosts, totalPages } = getAllBlogPosts(page, postsPerPage, searchQuery);
|
||||||
|
|
||||||
|
const noResults = blogPosts.length === 0; // Check if there are no results
|
||||||
|
|
||||||
res.render('index', {
|
res.render('index', {
|
||||||
title: `${process.env.OWNER_NAME} Blog`,
|
title: `${process.env.OWNER_NAME}'s Blog`,
|
||||||
blogPosts,
|
blogPosts,
|
||||||
currentPage: page,
|
currentPage: page,
|
||||||
totalPages,
|
totalPages,
|
||||||
searchQuery // Pass search query to the view
|
searchQuery, // Pass search query to the view
|
||||||
|
noResults // Pass this flag to indicate no results found
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// About Route
|
// About Route
|
||||||
app.get('/about', (req, res) => {
|
app.get('/about', (req, res) => {
|
||||||
res.render('about', { title: `About ${process.env.OWNER_NAME}` });
|
res.render('about', { title: `About ${process.env.OWNER_NAME}` });
|
||||||
|
@ -49,7 +49,11 @@
|
|||||||
<div class="container">
|
<div class="container">
|
||||||
|
|
||||||
<!-- Blog post list -->
|
<!-- Blog post list -->
|
||||||
<h2><%= searchQuery ? `Search results for "${searchQuery}"` : 'Recent Posts' %></h2>
|
<% if (noResults) { %>
|
||||||
|
<p><CENTER>Sorry, no blog posts found matching "<%= searchQuery %>"</CENTER></p>
|
||||||
|
<% } else { %>
|
||||||
|
<h2><%= searchQuery ? 'Search results for "' + searchQuery + '"' : 'Recent Posts' %></h2>
|
||||||
|
<% } %>
|
||||||
<ul class="list-group list-group-flush">
|
<ul class="list-group list-group-flush">
|
||||||
<% blogPosts.forEach(post => { %>
|
<% blogPosts.forEach(post => { %>
|
||||||
<li class="list-group-item d-flex justify-content-between align-items-center py-4">
|
<li class="list-group-item d-flex justify-content-between align-items-center py-4">
|
||||||
|
Loading…
Reference in New Issue
Block a user