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.

123 lines
4.3 KiB

4 years ago
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. function getURL(sqlData)
  26. {
  27. return "https://jrtechs.net/" + sqlData.category + "/" + sqlData.url;
  28. }
  29. routes.get('/recentSVG', (request, result) =>
  30. {
  31. sql.getRecentPosts(4).then((sqlData)=>
  32. {
  33. result.writeHead(200, {'Content-Type': 'image/svg+xml',
  34. 'Cache-Control': 'public, max-age=2678400',
  35. 'Vary': 'Accept-Encoding'});
  36. var res = `
  37. <svg width="806" height="200" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  38. <g>
  39. <title>background</title>
  40. <rect x="-1" y="-1" width="808" height="202" id="canvas_background" fill="#fff"/>
  41. <g id="canvasGrid" display="none">
  42. <rect id="svg_1" width="100%" height="100%" x="0" y="0" stroke-width="0" fill="url(#gridpattern)"/>
  43. </g>
  44. </g>
  45. <g>
  46. <title>Jrtechs</title>
  47. <a xlink:href="https://jrtechs.net">
  48. <text fill="#498FBE" stroke="#000" stroke-width="0" stroke-opacity="null" x="36.5" y="40.5" id="svg_6" font-size="24" font-family="Oswald, sans-serif" text-anchor="start" xml:space="preserve" font-weight="bold">Recent Blog Posts</text>
  49. </a>
  50. <a xlink:href="${getURL(sqlData[0])}">
  51. <text fill="#000000" stroke="#000" stroke-width="0" stroke-opacity="null" x="65.5" y="73.5" id="svg_7" font-size="20" font-family="Oswald, sans-serif" text-anchor="start" xml:space="preserve" font-weight="normal">- ${sqlData[0].name}</text>
  52. </a>
  53. <a xlink:href="${getURL(sqlData[1])}">
  54. <text fill="#000000" stroke="#000" stroke-width="0" stroke-opacity="null" x="65.5" y="106.5" id="svg_7" font-size="20" font-family="Oswald, sans-serif" text-anchor="start" xml:space="preserve" font-weight="normal">- ${sqlData[1].name}</text>
  55. </a>
  56. <a xlink:href="${getURL(sqlData[2])}">
  57. <text fill="#000000" stroke="#000" stroke-width="0" stroke-opacity="null" x="65.5" y="139.5" id="svg_7" font-size="20" font-family="Oswald, sans-serif" text-anchor="start" xml:space="preserve" font-weight="normal">- ${sqlData[2].name}</text>
  58. </a>
  59. <a xlink:href="${getURL(sqlData[3])}">
  60. <text fill="#000000" stroke="#000" stroke-width="0" stroke-opacity="null" x="65.5" y="172.5" id="svg_7" font-size="20" font-family="Oswald, sans-serif" text-anchor="start" xml:space="preserve" font-weight="normal">- ${sqlData[3].name}</text>
  61. </a>
  62. </g>
  63. </svg>`;
  64. result.write(res);
  65. result.end();
  66. }).catch((err)=>
  67. {
  68. result.status(404).json({error: 404}).end();
  69. })
  70. });
  71. routes.get('/posts', (request, result) =>
  72. {
  73. sql.getAllPosts().then((data)=>
  74. {
  75. result.json(data).end();
  76. }).catch((err)=>
  77. {
  78. result.status(500).json([]).end();
  79. });
  80. });
  81. routes.get('/preview/:postID', (request, result) =>
  82. {
  83. sql.getPostById(request.params.postID).then((sqlData)=>
  84. {
  85. renderPost.generateBlogPost(sqlData, 3).then((rendered)=>
  86. {
  87. result.json(rendered).end();
  88. });
  89. }).catch((err)=>
  90. {
  91. result.status(404).json({error: 404}).end();
  92. })
  93. });
  94. routes.get('/render/:postID', (request, result) =>
  95. {
  96. sql.getPostById(request.params.postID).then((sqlData)=>
  97. {
  98. renderPost.generateBlogPost(sqlData, -1).then((rendered)=>
  99. {
  100. result.json(rendered).end();
  101. });
  102. }).catch((err)=>
  103. {
  104. result.status(404).json({error: 404}).end();
  105. })
  106. });
  107. routes.get('*', (request, result) =>
  108. {
  109. result.json([]).end();
  110. });
  111. module.exports = routes;