Add error message when no posts are found
This commit is contained in:
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));
|
||||
}
|
||||
|
||||
if (blogFiles.length === 0) {
|
||||
return { blogPosts: [], totalPages: 0 }; // Return empty results if no files
|
||||
}
|
||||
|
||||
blogFiles.sort((a, b) => {
|
||||
const statA = fs.statSync(path.join(__dirname, 'markdown', a)).birthtime;
|
||||
const statB = fs.statSync(path.join(__dirname, 'markdown', b)).birthtime;
|
||||
@ -105,15 +109,19 @@ app.get('/', (req, res) => {
|
||||
const postsPerPage = 5;
|
||||
const { blogPosts, totalPages } = getAllBlogPosts(page, postsPerPage, searchQuery);
|
||||
|
||||
const noResults = blogPosts.length === 0; // Check if there are no results
|
||||
|
||||
res.render('index', {
|
||||
title: `${process.env.OWNER_NAME} Blog`,
|
||||
title: `${process.env.OWNER_NAME}'s Blog`,
|
||||
blogPosts,
|
||||
currentPage: page,
|
||||
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
|
||||
app.get('/about', (req, res) => {
|
||||
res.render('about', { title: `About ${process.env.OWNER_NAME}` });
|
||||
|
Reference in New Issue
Block a user