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.

75 lines
1.6 KiB

4 years ago
  1. const routes = require('express').Router();
  2. const sql = require('../../utils/sql');
  3. const renderPost = require('../../blog/renderBlogPost');
  4. routes.get('/getPostsIds/:category', (request, result) =>
  5. {
  6. if(request.params.category.length !== 1 &&
  7. request.params.category.length !== 2)
  8. {
  9. result.json("boo").end();
  10. return;
  11. }
  12. sql.getPostIds(request.params.category).then((sqlData)=>
  13. {
  14. var arr = [];
  15. for(var i = 5; i < sqlData.length; i++)
  16. {
  17. arr.push(sqlData[i].post_id)
  18. }
  19. result.json(arr).end();
  20. }).catch((err)=>
  21. {
  22. result.status(404).json({error: 404}).end();
  23. })
  24. });
  25. routes.get('/posts', (request, result) =>
  26. {
  27. sql.getAllPosts().then((data)=>
  28. {
  29. result.json(data).end();
  30. }).catch((err)=>
  31. {
  32. result.status(500).json([]).end();
  33. });
  34. });
  35. routes.get('/preview/:postID', (request, result) =>
  36. {
  37. sql.getPostById(request.params.postID).then((sqlData)=>
  38. {
  39. renderPost.generateBlogPost(sqlData, 3).then((rendered)=>
  40. {
  41. result.json(rendered).end();
  42. });
  43. }).catch((err)=>
  44. {
  45. result.status(404).json({error: 404}).end();
  46. })
  47. });
  48. routes.get('/render/:postID', (request, result) =>
  49. {
  50. sql.getPostById(request.params.postID).then((sqlData)=>
  51. {
  52. renderPost.generateBlogPost(sqlData, -1).then((rendered)=>
  53. {
  54. result.json(rendered).end();
  55. });
  56. }).catch((err)=>
  57. {
  58. result.status(404).json({error: 404}).end();
  59. })
  60. });
  61. routes.get('*', (request, result) =>
  62. {
  63. result.json([]).end();
  64. });
  65. module.exports = routes;