2017-06-27 137 views
-2

我正在使用node.js和mongodb创建我的第一个电子商务网站。到目前为止,我对所有事情都非常满意。这是我在后端留下的唯一问题。有时GET返回304而不是200

问题:当我在我的网站上点击“Add to Bag”时,它会将产品添加到我的购物袋中约95%的时间,并在我的终端中返回“GET 200”。另外5%的时间没有将产品添加到我的购物袋中,并在我的终端中返回“GET 304”。

显示我的终端响应适当添加至购物袋,然后失败添加至购物袋

totalQty: 14, 
    totalPrice: 5250, 
    add: [Function], 
    updateQty: [Function], 
    reduceByOne: [Function], 
    removeItem: [Function], 
    generateArray: [Function] } 
GET /add-to-bag/595258fadabeaab2357e0f1a 302 167.002 ms - 154 
GET /products/595258ccdabeaab2357e0f18 200 210.705 ms - 4970 
Bag { 
    items: { '595258fadabeaab2357e0f1a': { item: [Object], qty: 15, price: 5625 } }, 
    totalQty: 15, 
    totalPrice: 5625, 
    add: [Function], 
    updateQty: [Function], 
    reduceByOne: [Function], 
    removeItem: [Function], 
    generateArray: [Function] } 
GET /add-to-bag/595258fadabeaab2357e0f1a 302 157.734 ms - 154 
GET /products/595258ccdabeaab2357e0f18 304 197.984 ms - - 

购物袋型号

//gets the old bag 
module.exports = function Bag(oldBag) { 
    this.items = oldBag.items || {}; 
    this.totalQty = oldBag.totalQty || 0; 
    this.totalPrice = oldBag.totalPrice || 0; 
//adds new item to bag 
    this.add = function(item, id) { 
     //checks if item already exists in bag 
     var storedItem = this.items[id]; 
     if (!storedItem) { 
      storedItem = this.items[id] = {item: item, qty: 0, price: 0 }; 
     } 
     //increase quantity and adjusts price 
     storedItem.qty++; 
     storedItem.price = storedItem.item.price * storedItem.qty; 
     //updates total quantity and total price 
     this.totalQty++; 
     this.totalPrice+= storedItem.item.price; 
    }; 

    this.generateArray = function() { 
     var arr = []; 
     for (var id in this.items) { 
      arr.push(this.items[id]); 
     } 
     return arr; 
    }; 
}; 

购物袋路线

router.get('/add-to-bag/:id', function(req, res, next) { 
    var productId = req.params.id; 
    var bag = new Bag(req.session.bag ? req.session.bag : {items: {}}); 

    Product.findById(productId, function(err, product) { 
     if (err) { 
      return res.redirect('/'); 
     } 
     bag.add(product, product.id); 
     req.session.bag = bag; 
     console.log(req.session.bag); 
     res.redirect('back'); 
    }); 
}); 

我加入袋子视图

<!DOCTYPE html> 


<div class="container"> 

    <div class="row"> 
     {{# each products}}  
      <div class="col-md-4"> 
      <a href="/product/{{this._id}}"> 
       <div class="thumbnail"> 
        <img src="{{this.image}}"> 
        <div class="caption"> 
        <h3>{{this.name}}</h3> 
        <p>{{this.category.name}}</p> 
        <p>{{this.price}}</p> 
        <p><a href="/add-to-bag/{{this._id}}" class="btn btn-primary" align="center" role="button">Drop in Bag</a> </p> 
        </div> 
       </div> 
      </a> 
      </div> 
     {{/ each}} 
    </div> 
</div> 
+1

不应该...那不是一个GET请求,而是POST或PUT?你正在执行一个动作,而不是获得信息 –

回答

-1

TL; DR:使用POST请求而不是GET请求。

GET请求应该用于得到东西。一个GET请求不应该影响应用程序的状态(即服务器的状态)。

在你的情况下,添加一个项目到购物袋显然是一个修改服务器的状态

如果你不相信,check out this answer

一个GET以这种方式在HTTP协议定义。它应该是幂等和安全的。

如果您使用POST请求,它不仅可以解决您的问题,还可以防止其他一些可能的错误。 (它会更正确。)

+0

啊,这使得更多的意义,我会立即执行。对不起,你们投了票,这是很多的帮助。 – Troy

0

我不能确定你用什么堆栈来创建应用程序。它看起来像你使用Express.js做路由不过,我可以告诉你为什么你得到一个304

维基百科:

304未修改(RFC 7232)
指示自请求标头If-Modified-Since或If-None-Match指定的版本以来,资源未被修改。在这种情况下,由于客户端仍然具有先前下载的副本,所以不需要重新传输资源。[24]

304表示“嘿,还记得我给你最后的答案吗?它并没有改变”,因此您的浏览器将重播从缓存中收到的最后一个响应,而不必发生过的数据传输。

这意味着您的被添加了。但由于包中的数据完全相同,因此服务器只需发出一个304即可。而服务器只需发出一个304.

BTW:您的API不是Restful。我建议使用POST创建新记录,而不是向另一个URL发出GET。我建议阅读REST API设计。一旦你掌握了它,它是非常简单的。

+0

感谢您的帮助和指导。在继续之前,我会详细阅读REST API设计。 – Troy

相关问题