Personal blog written from scratch using Node.js, Bootstrap, and MySQL. https://jrtechs.net
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.

51 lines
1.1 KiB

  1. const routes = require('express').Router();
  2. const sql = require('../../utils/sql');
  3. const renderPost = require('../../blog/renderBlogPost');
  4. routes.get('/posts', (request, result) =>
  5. {
  6. sql.getAllPosts().then((data)=>
  7. {
  8. result.json(data).end();
  9. }).catch((err)=>
  10. {
  11. result.status(500).json([]).end();
  12. });
  13. });
  14. routes.get('/preview/:postID', (request, result) =>
  15. {
  16. sql.getPostById(request.params.postID).then((sqlData)=>
  17. {
  18. renderPost.generateBlogPost(sqlData, 3).then((rendered)=>
  19. {
  20. result.json(rendered).end();
  21. });
  22. }).catch((err)=>
  23. {
  24. result.status(404).json({error: 404}).end();
  25. })
  26. });
  27. routes.get('/render/:postID', (request, result) =>
  28. {
  29. sql.getPostById(request.params.postID).then((sqlData)=>
  30. {
  31. renderPost.generateBlogPost(sqlData, -1).then((rendered)=>
  32. {
  33. result.json(rendered).end();
  34. });
  35. }).catch((err)=>
  36. {
  37. result.status(404).json({error: 404}).end();
  38. })
  39. });
  40. routes.get('*', (request, result) =>
  41. {
  42. result.json([]).end();
  43. });
  44. module.exports = routes;