Advertisement
Guest User

Untitled

a guest
Dec 4th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. >>>>>>>>>>>>>>>>>>>>>>>>>> db-connection.js
  2. var mysql = require('mysql');
  3. var connection = mysql.createPool({
  4. host: 'localhost',
  5. user: 'root',
  6. password: '',
  7. database: 'movie_review'
  8. })
  9.  
  10. module.exports = connection;
  11.  
  12.  
  13. >>>>>>>>>>>>>>>>>>>>>>>>> moviesDB.js
  14. "use strict";
  15.  
  16. //This is to get the connection to the database
  17. var db = require('../db-connection'); //reference of db-connection.js
  18.  
  19. class MoviesDB
  20. {
  21. getAllMovies(callback)
  22. {
  23. var sql = "SELECT * FROM movie_review.movie";
  24.  
  25. //This is to call the built-in query function in the database connection
  26. db.query(sql,callback);
  27. }
  28. }
  29.  
  30. module.exports = MoviesDB;
  31.  
  32. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> routeMovies.js
  33. "use strict";
  34.  
  35. const movieController = require('../controllers/movieController');
  36.  
  37. function routeMovies (app)
  38. {
  39. //When the URl is http://localhost:8080/movies, and when HTTP
  40. //GET method is requested from the cloient, the get() function will
  41. // be used to call the getAllMovies() function in the controller.
  42. //The HTTP GET method is called in the getMovieData() function in
  43. //movies.js. The codes the called it is (open the movies.js file to see):
  44. // var request = new XMLHttpRequest();
  45. // request.open('GET', movie_url, true);
  46. app.route('/movies')
  47. .get(movieController.getAllMovies);
  48. }
  49.  
  50. module.exports = { routeMovies };
  51.  
  52.  
  53. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> movieController.js
  54. "use strict";
  55.  
  56. const MoviesDB = require('../models/MoviesDB');
  57.  
  58. var moviesDB = new MoviesDB();
  59.  
  60. function getAllMovies(request, respond)
  61. {
  62. //Call the getAllMovies() function in the MoviesDB class.
  63. moviesDB.getAllMovies(function(error, result)
  64. {
  65. if (error){
  66. respond.json(error);
  67. }
  68. else {
  69. respond.json(result);
  70. }
  71. });
  72. }
  73.  
  74. module.exports = { getAllMovies };
  75.  
  76.  
  77. >>>>>>>>>>>>>>>>>>>>>>>> server.js
  78. "use strict";
  79.  
  80. const express = require("express");
  81. const routeMovies = require('./routes/routeMovies');
  82. const bodyParser = require("body-parser");
  83. var app = express();
  84. var host = "127.0.0.1";
  85. var port = 8080;
  86. var startPage = "index.html";
  87.  
  88. app.use(express.static("./public"));
  89. app.use(bodyParser.urlencoded({ extended: true }));
  90. app.use(bodyParser.json());
  91.  
  92. routeMovies.routeMovies(app);
  93.  
  94. function gotoIndex(req, res) {
  95. console.log(req.params);
  96. res.sendFile(__dirname + "/" + startPage);
  97. }
  98.  
  99. app.get("/" + startPage, gotoIndex);
  100.  
  101. app.route("/");
  102.  
  103. var server = app.listen(port, host, function() {
  104. var host = server.address().address;
  105. var port = server.address().port;
  106.  
  107. console.log("Example app listening at http://%s:%s", host, port);
  108. });
  109.  
  110.  
  111.  
  112. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> app.js
  113. //var movie_url = "https://api.mlab.com/api/1/databases/jobs/collections/movies?apiKey=V0HsJe9lC8Q7naTJK7kS3ZfKnLJdrbQg&s=%7B%27availability%27:1%7D";
  114. var movie_url = "/movies";
  115. var movie_array = []; // This creates an empty movie array
  116. var movieCount = 0;
  117. var currentIndex = 0;
  118.  
  119. /* There are two categories: " Now Showing" and "Coming Soon". This variable states which
  120. category of movies should be listed when the home page is first loaded. */
  121. var category = "Now Showing";
  122.  
  123. var remote_comment = "https://api.mlab.com/api/1/databases/jobs/collections/comments";
  124.  
  125. //API key needed by Cloud DB API
  126. var remote_api_key = "?apiKey=V0HsJe9lC8Q7naTJK7kS3ZfKnLJdrbQg&s={'datePosted':-1}";
  127.  
  128. var comment_url = remote_comment + remote_api_key;
  129. var comment_array = []; // This creates an empty comment array
  130.  
  131. var popcornBWImage = 'images/popcorn_bw.png';
  132. var popcornImage = 'images/popcorn.png';
  133. var rating = 0;
  134.  
  135. //This function is to display a modal
  136. //whenever the user clicks on "About" link on the nav bar
  137. function showAbout() {
  138. var messageModal = new Modal(document.getElementById("messageModal"));
  139. document.getElementById("message").innerHTML = "This is an academic website teaching students on fundamentals of various web technologies such as CSS, HTML, Javascript and Restful Web APIs<br><br>Please use Firefox or Chrome";
  140. document.getElementById("nowMenu").classList.remove("active");
  141. document.getElementById("comingMenu").classList.remove("active");
  142. document.getElementById("aboutMenu").classList.add("active");
  143. messageModal.show();
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement