javascript - Express.js CRUD – Chrome 中的 DELETE 后 GET 挂起

标签 javascript node.js google-chrome express

我正在使用一个非常简单的express.js 4.0.0 mongoose CRUD应用程序/api。我正在使用 postman 来测试它。我通过发布在数据库中创建了一些条目。现在,当我尝试删除其中一个时,它似乎可以工作,但此后我第一次使用 GET 时, postman 挂起。同样的情况也发生在浏览器中。它只是不断显示加载微调器。 问题不在于 mongoose,因为当我使用 GET 路由时,remove() 可以工作。

更新 这似乎是 chrome 的问题(v. 36.0.1985.125),在 Firefox 和 Sarafi 中它可以工作。那么,也许这是一个缓存问题?我尝试在 postman 中设置标题,如建议的 here但这没有帮助。

这是代码:

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' });
        });
    });

最佳答案

我在这里找到了这个问题的解决方案:https://github.com/strongloop/express/issues/2069 这似乎是由 Sophos Anti-Virus 引起的错误。为了避免这种情况,您可以在 Sophos 的首选项中禁用相关网站的“网络保护”。

关于javascript - Express.js CRUD – Chrome 中的 DELETE 后 GET 挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25229130/

相关文章:

node.js - 异步和解析的问题

node.js - 构建模块时,我们在哪里保存演示应用程序依赖项?

google-chrome - Visual Studio 2013 native cordova 应用程序构建错误

javascript - 如何在固定高度和宽度的灯箱中显示大小图像 2

javascript - 如何使用 Puppeteer 等待事件触发的页面重新加载?

javascript - 使用 Flow Router 和 Meteor.js 处理 "no data"场景

javascript FileReader 上传 'Request Entity Too Large'

html - Chrome 忽略 <button> css

php - 跟踪网站多个页面的信息

javascript - 如何使 Raphael js 打印方法在本地计算机上适用于法语、德语等字符?