Compare commits
2 commits
1efee5a27c
...
d03c86ef42
Author | SHA1 | Date | |
---|---|---|---|
![]() |
d03c86ef42 | ||
![]() |
b8596b2296 |
2 changed files with 63 additions and 0 deletions
45
server/routes/posts.js
Normal file
45
server/routes/posts.js
Normal file
|
@ -0,0 +1,45 @@
|
|||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { getAllPosts, getPostBySlug } = require('../utils/markdownParser');
|
||||
|
||||
// Get all posts
|
||||
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const includeDrafts = req.query.includeDrafts === 'true';
|
||||
const posts = await getAllPosts(includeDrafts);
|
||||
|
||||
// Return only necessary data for listing
|
||||
const postsListing = posts.map(({ title, description, date, tags, slug, excerpt }) => ({
|
||||
title,
|
||||
description,
|
||||
date,
|
||||
tags,
|
||||
slug,
|
||||
excerpt
|
||||
}));
|
||||
|
||||
res.json(postsListing);
|
||||
} catch (error) {
|
||||
console.error('Error fetching posts:', error);
|
||||
res.status(500).json({ error: 'Failed to fetch posts' });
|
||||
}
|
||||
});
|
||||
|
||||
// Get post by slug
|
||||
router.get('/:slug', async (req, res) => {
|
||||
try {
|
||||
const post = await getPostBySlug(req.params.slug);
|
||||
|
||||
if (post.draft && process.env.NODE_ENV === 'production') {
|
||||
return res.status(404).json({ error: 'Post not found' });
|
||||
}
|
||||
|
||||
res.json(post);
|
||||
} catch (error) {
|
||||
console.error(`Error fetching post ${req.params.slug}:`, error);
|
||||
res.status(404).json({ error: 'Post not found' });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
18
server/routes/projects.js
Normal file
18
server/routes/projects.js
Normal file
|
@ -0,0 +1,18 @@
|
|||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
|
||||
// Get all projects
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const projectsPath = path.join(__dirname, '../content/projects/projects.json');
|
||||
const projects = await fs.readJSON(projectsPath);
|
||||
res.json(projects);
|
||||
} catch (error) {
|
||||
console.error('Error fetching projects:', error);
|
||||
res.status(500).json({ error: 'Failed to fetch projects' });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
Loading…
Reference in a new issue