Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. app.get('/courses', function (req, res) {
  2.     var id = req.param('id'); // some variable from request
  3.     var coursesResponse = getCourses(id); // example for getting courses
  4.  
  5.     if (!!courses && courses.length >= 0) { // if courses isn't undefined and length is greater >= 0 (example condition)
  6.  
  7.         // most important function
  8.         res.json({
  9.             success: true,
  10.             courses: coursesResponse
  11.         });
  12.     } else {
  13.         res.status(500).json({
  14.             success: false,
  15.             message: 'Internal server error while fetching courses'
  16.         })
  17.     }
  18.  
  19.     }
  20. )
  21.  
  22.  
  23. // on client side. using jquery
  24.  
  25. // make a ajax request first and then parse the data
  26. var courses = $.parseJSON(data);
  27.  
  28. // now loop through the courses array and show the values of array in a div with id 'courses'.
  29. $.each(courses, function(index, value) {
  30.     $("#courses").append("<p>" + value.courseCode + " -- " + value.courseTitle + "</p>");
  31. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement