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.

131 lines
4.5 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. /**
  30. * Returns an svg containing the recent blog posts
  31. *
  32. * Used on github profile readme:
  33. * https://github.com/jrtechs
  34. */
  35. routes.get('/recentSVG', (request, result) =>
  36. {
  37. sql.getRecentPosts(4).then((sqlData)=>
  38. {
  39. result.writeHead(200, {'Content-Type': 'image/svg+xml',
  40. 'Cache-Control': 'no-cache',
  41. 'Vary': 'Accept-Encoding'});
  42. var res = `
  43. <svg width="800" height="200" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  44. <g>
  45. <title>background</title>
  46. <rect x="-1" y="-1" width="808" height="202" id="canvas_background" fill="#fff"/>
  47. <g id="canvasGrid" display="none">
  48. <rect id="svg_1" width="100%" height="100%" x="0" y="0" stroke-width="0" fill="url(#gridpattern)"/>
  49. </g>
  50. </g>
  51. <g>
  52. <title>Jrtechs</title>
  53. <a xlink:href="https://jrtechs.net">
  54. <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>
  55. </a>
  56. <a xlink:href="${getURL(sqlData[0])}">
  57. <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>
  58. </a>
  59. <a xlink:href="${getURL(sqlData[1])}">
  60. <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>
  61. </a>
  62. <a xlink:href="${getURL(sqlData[2])}">
  63. <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>
  64. </a>
  65. <a xlink:href="${getURL(sqlData[3])}">
  66. <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>
  67. </a>
  68. </g>
  69. </svg>`;
  70. result.write(res);
  71. result.end();
  72. }).catch((err)=>
  73. {
  74. console.log(err);
  75. result.status(404).json({error: 404}).end();
  76. })
  77. });
  78. routes.get('/posts', (request, result) =>
  79. {
  80. sql.getAllPosts().then((data)=>
  81. {
  82. result.json(data).end();
  83. }).catch((err)=>
  84. {
  85. result.status(500).json([]).end();
  86. });
  87. });
  88. routes.get('/preview/:postID', (request, result) =>
  89. {
  90. sql.getPostById(request.params.postID).then((sqlData)=>
  91. {
  92. renderPost.generateBlogPost(sqlData, 3).then((rendered)=>
  93. {
  94. result.json(rendered).end();
  95. });
  96. }).catch((err)=>
  97. {
  98. result.status(404).json({error: 404}).end();
  99. })
  100. });
  101. routes.get('/render/:postID', (request, result) =>
  102. {
  103. sql.getPostById(request.params.postID).then((sqlData)=>
  104. {
  105. renderPost.generateBlogPost(sqlData, -1).then((rendered)=>
  106. {
  107. result.json(rendered).end();
  108. });
  109. }).catch((err)=>
  110. {
  111. result.status(404).json({error: 404}).end();
  112. })
  113. });
  114. routes.get('*', (request, result) =>
  115. {
  116. result.json([]).end();
  117. });
  118. module.exports = routes;