0

我正在向返回 JSON 对象的 API 发出 POST 请求。在我添加 express.sessions 之前,一切都运行良好。现在,POST 请求仍然给出 200 状态,但在 2 分钟内(超时时)没有返回任何内容。API被成功调用,后端的行为一切正常,但我无法从服务器检索响应。事实上,它甚至不调用 .error() 函数。它只是超时而不做任何事情。

最奇怪的部分:当我第二次单击发出 POST 请求的链接时(紧随第一次),请求和响应完全按预期执行,没有任何问题。第一个请求总是超时。

当我删除 express.sessions 时,一切都按预期工作。

我在 Node 上使用 Express,在前端使用 AngularJS。

这是 POST 请求的代码:

正在提出请求。这是在 Angular 控制器中。(行为:没有记录任何内容。它没有达到 success() 或 error() 但在 2 分钟后超时):

  $http.post('/api/add_highlight', newHighlight).
    success(function(id){
      console.log('response from server: ' + id)
    }).
    error(function(id){
      console.log('server response failed')
    })

API(行为:成功将条目保存到数据库中。控制台日志均显示成功,后端行为正常。):

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(当第二行被注释掉时,一切都按预期运行):

  app.use(express.cookieParser());
  app.use(express.session({store: new RedisStore(), secret: '[...]'}));

如果您以前遇到过类似的事情或有任何想法,请告诉我。

我高度怀疑这是我使用 Angular 的问题,因为来自示例的另一个 POST 请求运行良好。但我确实怀疑这也与我应该如何在 Angular 中处理会话有关,因为当我在 app.js 中注释掉会话行时,一切正常。

谢谢!

4

1 回答 1

0

明白了——这很愚蠢。我没有返回我的 res.json 和 res.send

于 2013-08-03T19:05:48.927 回答