You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
2.5 KiB
102 lines
2.5 KiB
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 = '<h1>Posts List</h1><ul>'; |
|
posts.forEach(post => { |
|
html += `<li><a href="/viewPost?filename=${post}">${post.replace('.yaml', '')}</a></li>`; |
|
}); |
|
html += '</ul>'; |
|
|
|
res.send(html); |
|
}); |
|
}); |
|
|
|
router.get('/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; |
|
} |
|
let post = matter(stdout) |
|
let { title, author, timestamp } = post.data |
|
let html="" |
|
|
|
if ( title ) { |
|
html += `<h1>${title}</h1>` |
|
} |
|
|
|
if ( author && timestamp ) { |
|
let date = new Date(parseInt(timestamp)).toLocaleString() |
|
html += `<header>Created by ${author} on ${date}</header>` |
|
} |
|
html += '<hr/>' |
|
html += `<p>${post.content}</p>` |
|
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 |
|
|
|
|