Ignore:
Timestamp:
07/01/13 03:11:23 (12 years ago)
Author:
hendrikvanantwerpen
Message:

Reorganized app and fixed some style issues.

Was looking for a bug where the proxy would not forward requests to the
database. This is somehow magically resolved now.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Dev/trunk/src/server/app.js

    r467 r470  
    4343    app.use(express.favicon());
    4444
     45    // cookies and session
     46    app.use('/api/login',express.bodyParser());
    4547    app.use(express.cookieParser());
    46     app.use(express.bodyParser());
    4748    app.use(express.session({ secret: "quasi experimental design" }));
    4849
    49     // initialize passport
     50    // passport
    5051    app.use(passport.initialize());
    5152    app.use(passport.session());
     53    function ensureAuthenticated(req,res,next){
     54        if (!req.user) {
     55            return res.send(401,{error:"Login before accessing API."});
     56        } else {
     57            return next();
     58        }
     59    }
     60    function returnUser(req,res) {
     61        res.send(200, req.user);
     62    }
    5263
    5364    // static resources
     
    6273    });
    6374
    64    
    65     // url to login (might work on others as well?)
    66     // you should then have a session to work with
    67     // should return a user info object
    68     function returnUser(req,res) {
    69         res.send(200, req.user);
    70     }
     75
     76    // post to this url to login
    7177    app.post(
    7278        '/api/login',
     
    7480        returnUser);
    7581
    76     // ensure we're authenticated on API calls
    77     function ensureAuthenticated(req,res,next){
    78         if (!req.user) {
    79             return res.send(401,{error:"Login before accessing API."});
    80         } else {
    81             return next();
    82         }
    83     }
    84    
     82    // return the info for the current logged in user
    8583    app.get(
    8684        '/api/login',
     
    8886        returnUser);
    8987
     88    // explicitly logout this user
    9089    app.post(
    9190        '/api/logout',
     
    9695        });
    9796
     97    // data is proxied to couch
     98    app.use('/api/data', ensureAuthenticated);
     99    app.use('/api/data', proxy(settings.couchDbURL));
     100
     101    // generate CSV download of responses
    98102    app.get(
    99103        '/api/surveyRuns/:id/responses.csv',
     
    109113        });
    110114   
    111     // forward to couch
    112     app.use('/api/data', ensureAuthenticated);
    113     app.use('/api/data', proxy(settings.couchDbURL));
    114 
    115115    return app;
    116116
Note: See TracChangeset for help on using the changeset viewer.