Changeset 466 for Dev/trunk/src/server/app.js
- Timestamp:
- 06/26/13 14:43:57 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Dev/trunk/src/server/app.js
r464 r466 4 4 var fs = require("fs"); 5 5 var path = require("path"); 6 var proxy = require("./ simple-http-proxy");6 var proxy = require("./util/simple-http-proxy"); 7 7 var _ = require("underscore"); 8 8 … … 31 31 } 32 32 })); 33 passport.serializeUser(function(user, done) { 34 done(null, user.username); 35 }); 36 passport.deserializeUser(function(id, done) { 37 done(null, {username: id}); 38 }); 33 39 34 40 var app = express(); … … 56 62 }); 57 63 64 58 65 // url to login (might work on others as well?) 59 66 // you should then have a session to work with 60 app.post('/api/login'); 67 // should return a user info object 68 function returnUser(req,res) { 69 res.send(200, req.user); 70 } 71 app.post( 72 '/api/login', 73 passport.authenticate('local'), 74 returnUser); 61 75 76 // ensure we're authenticated on API calls 77 app.use('/api', function(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 85 app.get( 86 '/api/login', 87 returnUser); 88 89 app.post( 90 '/api/logout', function(req,res){ 91 req.logout(); 92 res.send(200,{}); 93 }); 94 95 app.get('/api/surveyRun/:id/response/csv', 96 function(req, res) { 97 var id = req.params.id; 98 // query CouchDB and build the CSV file 99 res.set({ 100 'Content-Type': 'text/csv', 101 'Content-Disposition': 'attachment; filename=responses-'+id+'.csv' 102 }); 103 res.send(200, "Response for surveyRun "+id); 104 }); 105 62 106 // forward to couch 63 app.use('/ data/couch', proxy(settings.couchDbURL));107 app.use('/api/data', proxy(settings.couchDbURL)); 64 108 65 109 return app;
Note: See TracChangeset
for help on using the changeset viewer.