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.

70 lines
1.7 KiB

  1. const mysql = require('mysql');
  2. const con = mysql.createConnection({
  3. host: "localhost",
  4. user: "blog_user",
  5. password: "password",
  6. database: "blog_name"
  7. });
  8. con.connect(function(err) {
  9. if (err) throw err;
  10. console.log("Connected!");
  11. });
  12. module.exports=
  13. {
  14. /**
  15. * Function used to query the database for records
  16. *
  17. * @param sqlStatement
  18. * @returns {Array}
  19. */
  20. fetch : function(sqlStatement)
  21. {
  22. con.query(sqlStatement, function (err, result)
  23. {
  24. if (err) throw err;
  25. return result;
  26. });
  27. return [];
  28. },
  29. /**
  30. * Function used to use insert statements into the database
  31. *
  32. * @param sqlStatement
  33. * @return the id of the new record - if there is one
  34. */
  35. insert : function(sqlStatement)
  36. {
  37. con.connect(function(err)
  38. {
  39. if (err) throw err;
  40. con.query(sqlStatement, function (err, result)
  41. {
  42. if (err) throw err;
  43. return result.insertId;
  44. });
  45. });
  46. return 0;
  47. },
  48. getPost : function(requestURL)
  49. {
  50. var splitURL = requestURL.split("/");
  51. var result_category = this.fetch("select * from categories " +
  52. "where url='" + splitURL[1] + "'");
  53. if(result_category.length != 0)
  54. {
  55. var result_posts = this.fetch("select * from posts where" +
  56. "category_id='" + result_category[0].category_id + "' " +
  57. "and url='" + splitURL[2] + "'");
  58. if(result_posts.length != 0)
  59. {
  60. return result_posts[0];
  61. }
  62. }
  63. return 0;
  64. }
  65. };