1

I am playing around with a really simple express.js 4.0.0 mongoose CRUD application / api. I am using postman to test it. I created some entries in the database by posting. Now when I try to delete one of them it seems to work but the first time I use GET after that, postman hangs. The same happens in the browser. It just keeps showing the loading spinner. The problem is not with mongoose because when I use a GET route, the remove() works.

UPDATE This seems to be an issue with chrome ( v. 36.0.1985.125 ) In Firefox and Sarafi it works. So, maybe this is a caching issue? I tried setting headers in postman like suggested here but it did not help.

Here is the code:

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser());
var router = express.Router();


/* 
UPDATE
Suspecting it is a caching issues in chrome, now using this middleware to prevent caching 
*/

router.use(function(req, res, next) {

  res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
  res.header('Expires', '-1');
  res.header('Pragma', 'no-cache');
  next();

});
/* UPDATE */

app.use('/', router);

router.get('/', function(req, res){
    res.send('Homepage');
});

var server = app.listen(3000, function(){
    console.log('Listening to port %d', server.address().port);
});

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var Schema = mongoose.Schema;
var BearSchema = new Schema({
    name: String
});
var Bear = mongoose.model('Bear', BearSchema);

router.route('/bears')
    .post(function(req, res){
        var bear = new Bear();
        bear.name = req.body.name;
        bear.save(function(err){
            if(err){
                res.send(err);
                return;
            }
            res.json({ message: 'Bear created' });
        });
    })
    .get(function(req, res){
        Bear.find(function(err, bears){
            if(err){
                res.send(err);
                return;
            }
            res.json(bears);
        });
    });

router.route('/bears/:bear_id')
    .delete(function(req, res){
        Bear.remove({
            _id: req.params.bear_id
        }, function(err, num){
            if(err){
                res.send(err);
                return;
            }
            if( num === 0 ){
                res.json({ message: 'Nothing deleted' });
                return;
            }
            res.json({ message: 'Successfully deleted' });
        });
    });
Community
  • 1
  • 1
gang
  • 1,758
  • 2
  • 23
  • 36
  • if you suspect chrome caching, run your tests with the dev tools open, and it's "disable cache while dev tools is open" setting checked, as well as a general `app.use` that adds a no-cache header to `res..headers` – Mike 'Pomax' Kamermans Aug 10 '14 at 16:19
  • Thanks. I did all of this now but the request still stays 'pending' in devtools. I updated the code with the middleware that I am using now. – gang Aug 10 '14 at 16:30
  • Have you tried c&p the code from scotch.io(I think this is from there) and seeing if it works? – c0d3junk13 Aug 10 '14 at 18:14
  • Hello. I just cloned the code from the tutorial and it is exactly the same. Any Ideas? – gang Aug 11 '14 at 07:33
  • Dumb question, but are you using [method-override](https://github.com/expressjs/method-override) ? If not, that could be your problem. Method Override is no more included in Express since v4.0. – Waldo Jeffers Aug 11 '14 at 12:39
  • You can see from the code that I am not using method-override. The requests are made through postman and the delete route gets hit. method-override is only necessary if your client does not support DELETE requests, right? The only problem is that a get after a delete stays "pending". – gang Aug 11 '14 at 12:45
  • Maybe someone could try this simple app on his machine and see if he has the same problem in chrome. – gang Aug 11 '14 at 13:00
  • Yeah, but that wouldn't work. We don't have your DB. I suggest you use [morgan logger](https://www.npmjs.org/package/morgan) to see what happens with your request. What do you mean by "it seems to work" ? Do have a response in Postman ? Plus, I might be wrong but I would still try to use `method-override`. It only takes one line of code, and you'd be sure I'm wrong. – Waldo Jeffers Aug 12 '14 at 08:38
  • All you need for it to work is mongo running. Morgan logger does not indicate that anything went wrong. By "it seems to work" I mean that the delete route gets hit and the record in the database gets removed. I get the expected response { message: "Successfully deleted" }. Method override works like expected but it is not really a solution to the problem which really seems to be with chrome and not express. – gang Aug 13 '14 at 19:11

1 Answers1

0

I found the solution to this problem here: https://github.com/strongloop/express/issues/2069 It appears to be a bug caused by Sophos Anti-Virus. To avoid it you can disable "web protection" for the website in question in the preferences of Sophos.

gang
  • 1,758
  • 2
  • 23
  • 36