const express = require('express'); const matter = require('gray-matter'); const router = express.Router(); const { exec } = require('child_process'); let dir = process.argv[2] || '.'; router.get('/', (req, res) => { res.send('Welcome to our minimalist web application!') }); router.get('/api/posts', (req, res) => { let page = req.query.page || 1; let limit = req.query.limit || 10; exec(`./routes/listPosts ${dir} ${page} ${limit}`, (error, stdout, stderr) => { if (error) { console.log(`error: ${error.message}`); return; } if (stderr) { console.log(`stderr: ${stderr}`); return; } let posts = stdout.split('\n').filter(Boolean); res.json(posts); }); }); router.get('/posts', (req, res) => { let page = req.query.page || 1; let limit = req.query.limit || 10; exec(`./routes/listPosts ${dir} ${page} ${limit}`, (error, stdout, stderr) => { if (error) { console.log(`error: ${error.message}`); return; } if (stderr) { console.log(`stderr: ${stderr}`); return; } let posts = stdout.split('\n').filter(Boolean); let html = '
${post.content}
` res.send(html); }); }); router.get('/api/viewPost', (req, res) => { let { filename } = req.query; exec(`./routes/viewPost --filename=${filename}`, (error, stdout, stderr) => { if (error) { console.log(`error: ${error.message}`); return; } if (stderr) { console.log(`stderr: ${stderr}`); return; } res.json(matter(stdout)); }); }); /*app.listen(3000, () => { console.log('Server is running on port 3000'); }); */ module.exports = router