I am making a POST request in angular to an API that returns a JSON object. Everything was working perfectly until I added express.sessions to the mix. Now, the POST request still gives a 200 status, but doesn't return anything for 2 minutes (when it times out). The API gets called successfully and the behavior on the backend is all normal, but I can't retrieve the response from the server. In fact, it doesn't even call the .error() function. It just times out without doing anything.
The strangest part: when I click the link that makes the POST request a second time (immediately following the first), the request and response execute exactly as expected and nothing is wrong. The first request always times out.
When I remove express.sessions, everything works as expected as well.
I am using Express on Node, AngularJS on the frontend.
Here's the code for the POST request:
Request being made. This is within an Angular controller. (Behavior: Nothing gets logged. It doesn't reach success() or error() but times out after 2min):
$http.post('/api/add_highlight', newHighlight).
success(function(id){
console.log('response from server: ' + id)
}).
error(function(id){
console.log('server response failed')
})
API (Behavior: Successfully saves entry into database. Console logs all show success and behavior is normal on the backend.):
exports.add_highlight = function(req, res) {
console.log('API has received request to add highlight')
var hl = new models.highlights ({
start: req.body.start
, end: req.body.end
, contents: req.body.contents
})
console.log(hl._id)
hl.save(function(err){
if (!err) {
console.log('successfully saved highlight to mongo.')
res.json(hl._id)
}
res.send('failed')
})
};
app.js (When the second line is commented out, everything behaves as expected):
app.use(express.cookieParser());
app.use(express.session({store: new RedisStore(), secret: '[...]'}));
Please let me know if you have encountered something similar before or have any ideas.
I highly suspect it's an issue with my usage of Angular, because another POST request from an example works perfectly fine. But I do suspect it also has something to do with how I'm supposed to work with sessions in Angular, because when I comment out the session line in app.js, everything works fine too.
Thanks!
Got it- it was very silly. I didn't return my res.json and res.send