set a maxAge timeout on static files and setting up an error log for res.sendfile
[oweals/finalsclub.git] / app.js
diff --git a/app.js b/app.js
index ebe2d0c1c84dd436a418251ea9a10970b5348726..ebaaecc06762ba7483053983949ba77cd8e79ee4 100644 (file)
--- a/app.js
+++ b/app.js
@@ -161,7 +161,7 @@ app.configure(function(){
   // requests that otherwise aren't handled by default.
   app.use( express.methodOverride() );
   // Static files are loaded when no dynamic views match.
-  app.use( express.static( __dirname + '/public' ) );
+  app.use( express.static( __dirname + '/public', {maxAge: 900000} ) );
 
   // Sets the routers middleware to load after everything set
   // before it, but before static files.
@@ -337,33 +337,6 @@ function loadSchool( req, res, next ) {
   });
 }
 
-function loadSchoolSlug( req, res, next ) {
-  var user    = req.user;
-  var schoolSlug = req.params.slug;
-
-  console.log("loading a school by slug");
-  //console.log(schoolSlug);
-
-  School.findOne({ 'slug': schoolSlug }, function( err, school ) {
-    console.log( school );
-    if( school ) {
-      req.school = school;
-
-      // If a school is found, the user is checked to see if they are
-      // authorized to see or interact with anything related to that
-      // school.
-      next()
-      //school.authorize( user, function( authorized ){
-        //req.school.authorized = authorized;
-        //next();
-      //});
-    } else {
-      // If no school is found, display an appropriate error.
-      sendJson(res,  {status: 'not_found', message: 'Invalid school specified!'} );
-    }
-  });
-}
-
 // loadSchool is used to load a course by it's id
 function loadCourse( req, res, next ) {
   var user                     = req.user;
@@ -468,7 +441,11 @@ function checkAjax( req, res, next ) {
   if ( req.xhr ) {
     next();
   } else {
-    res.sendfile( 'public/index.html' );
+    res.sendfile( 'public/index.html', function(err){
+      if(err){
+        console.log(err);
+      }
+    });
   }
 }
 
@@ -496,27 +473,11 @@ function sendJson( res, obj ) {
   res.json(obj);
 }
 
-// Routes
-// The following are the main CRUD routes that are used
-// to make up this web app.
-
-// Homepage
-// Public
-/*
-app.get( '/', loadUser, function( req, res ) {
-  log3("get / page");
-
-  res.render( 'index' );
-});
-*/
 
 // Schools list
-// Used to display all available schools and any courses
-// in those schools.
+// Used to display all available schools
 // Public with some private information
 app.get( '/schools', checkAjax, loadUser, function( req, res ) {
-  sys.puts('loading schools');
-  console.log(req.user);
   var user = req.user;
 
   var schoolList = [];
@@ -539,7 +500,8 @@ app.get( '/schools', checkAjax, loadUser, function( req, res ) {
                   description: s.description,
                   url: s.url,
                   slug: s.slug,
-                  courses: s.courses_length
+                  courses: s.courses_length,
+                  courseNum: s.courseNum
                 };
                 return school;
               })
@@ -558,61 +520,24 @@ app.get( '/schools', checkAjax, loadUser, function( req, res ) {
 app.get( '/school/:name', checkAjax, loadUser, loadSchool, function( req, res ) {
   var school = req.school;
   var user = req.user;
-  var courses;
-  console.log( 'loading a school by school/:id now name' );
-
-  //school.authorize( user, function( authorized ) {
-    // This is used to display interface elements for those users
-    // that are are allowed to see th)m, for instance a 'New Course' button.
-    //var sanitizedSchool = school.sanitized;
-    var sanitizedSchool = {
-      _id: school.id,
-      name: school.name,
-      description: school.description,
-      url: school.url
-    };
-    //sanitizedSchool.authorized = authorized;
-    // Find all courses for school by it's id and sort by name
-    Course.find( { 'school' : school._id } ).sort( 'name', '1' ).run( function( err, courses ) {
-      // If any courses are found, set them to the appropriate school, otherwise
-      // leave empty.
-      sys.puts(courses);
-      if( courses.length > 0 ) {
-        courses = courses.filter(function(course) {
-          if (!course.deleted) return course;
-        }).map(function(course) {
-          return course.sanitized;
-        });
-      } else {
-        school.courses = [];
-      }
-      sanitizedSchool.courses = courses;
-      sys.puts(courses);
-
-      // This tells async (the module) that each iteration of forEach is
-      // done and will continue to call the rest until they have all been
-      // completed, at which time the last function below will be called.
-      sendJson(res, { 'school': sanitizedSchool, 'user': user.sanitized })
-    });
-  //});
-});
-
-// FIXME: version of the same using school slugs instead of ids
-// TODO: merge this with the :id funciton or depricate it
-app.get( '/schoolslug/:slug', checkAjax, loadUser, loadSchoolSlug, function( req, res ) {
-  var school = req.school;
-  var user = req.user;
-  console.log( 'loading a schoolslug/:slug' );
 
+  console.log("Loading a school");
   school.authorize( user, function( authorized ) {
     // This is used to display interface elements for those users
     // that are are allowed to see th)m, for instance a 'New Course' button.
     var sanitizedSchool = school.sanitized;
+    //var sanitizedSchool = {
+    //  _id: school.id,
+    //  name: school.name,
+    //  description: school.description,
+    //  url: school.url
+    //};
     sanitizedSchool.authorized = authorized;
     // Find all courses for school by it's id and sort by name
     Course.find( { 'school' : school._id } ).sort( 'name', '1' ).run( function( err, courses ) {
       // If any courses are found, set them to the appropriate school, otherwise
       // leave empty.
+
       if( courses.length > 0 ) {
         sanitizedSchool.courses = courses.filter(function(course) {
           if (!course.deleted) return course;
@@ -620,8 +545,9 @@ app.get( '/schoolslug/:slug', checkAjax, loadUser, loadSchoolSlug, function( req
           return course.sanitized;
         });
       } else {
-        sanitizedSchool.courses = [];
+        school.courses = [];
       }
+
       // This tells async (the module) that each iteration of forEach is
       // done and will continue to call the rest until they have all been
       // completed, at which time the last function below will be called.