【问题标题】:azure node.js request, response - seeking some clarificationazure node.js 请求,响应 - 寻求一些澄清
【发布时间】:2016-01-26 11:05:02
【问题描述】:

我正在使用 Azure 移动服务-NodeJS 后端,在编写它时,我总是面临这个疑问 - 让我用下面的代码 sn-p 解释一下

//--------------------------------------------------------------------------
function addUserToDB(request, response){
  ///some code here
  var	theUser = request.user;

  ///get the user's entity object
  try {
    objAppUser = buildAppUserEntityObj(theUser, request); //for simplicity sake, lets say this is not asynchronous function
  }
  catch (err) {
    console.log ('error in addUserToDB when calling buildAppUserEntityObj');  //****????****
    request.respond(statusCodes.BAD_REQUEST, err);
    return; // ##????## is a 'return' needed here to avoid the execution of the code below,  or should I assume that the function will return once request is responded (request.respond) in above line.
  }

  ....code to add userEntity to DB
  //some more code in case of successful try above, can I assume there is no way this code will be reached in case of error in the above try-catch
  //       ofcourse I can move this code in the 'try' block above, but I am just trying to understand what happens if above try ends in catch block for some reason and there is no 'return' at the end that catch block.
}

//--------------------------------------------------------------------------
function buildAppUserEntityObj(user, request) {
  if ( user.level === 'anonymous' ) { //ideally this would be called in above function, but I am putting this here just to throw an example.
    console.error('Anonymous User' );
    request.respond(statusCodes.BAD_REQUEST, message);  //will this request.respond will send the response to client immediately, or will it be passed on as error the try-catch of above 'addUserToDB' function 
    return; // ##????## also, is 'return' needed here to avoid the execution of the code below, 
  }

  ....code to build a User entity object based on some business logic
}

//--------------------------------------------------------------------------

我想,这一切都归结为三个问题:
1.这两个地方是否需要'return'(上面两个函数中用##????##标记?
2. 如果 user.level === 'anonymous'
是否会记录消息(由 //****????**** 标记) 3. request.respond 与 response.send 有什么区别?

我相信这些疑问是因为我缺乏全面的 expressJS 知识,所以当我再次浏览 azure/express.js 文档时,我想我会在这里向专家社区提出我的疑问以获得更清晰的解释。

非常感谢。

【问题讨论】:

    标签: node.js azure express azure-mobile-services


    【解决方案1】:

    首先

    在第二个returnbuildAppUserEntityObj函数的insode,我相信你希望它是:

    throw new Error("Anonymous user is not allowed")
    

    否则,即使用户是匿名的,您的catch 代码也永远不会执行。


    你需要第一个return;,否则会继续执行下面的代码。

    第二

    如果您修复了第一段中描述的代码,则会记录消息。

    第三

    标准 Node.js http module 中没有 request.respond。你能澄清一下,你使用的是什么模块?无论如何,该模块的 API 都会回答您的问题。

    【讨论】:

    • 感谢 alandarev,我相信 request.respond 来自 azure 模块,我是从 azure 移动服务文档中挑选出来的。但是假设我用 response.send 替换它,在“buildAppUserEntityObj”函数中,在那之后我还需要一个“return”吗?我的意思是,我想的不是你建议的“抛出新错误”,如果我在那里调用 response.send,响应会立即发送到客户端,而不需要进一步执行服务器端代码,所以不需要我抛出错误以传递给第一个函数。
    • 不,即使发送了响应,代码也会继续执行……此外,如果响应被发送并关闭,进一步的代码可能会引发套接字异常,抱怨它过早关闭。
    猜你喜欢
    • 2023-03-23
    • 1970-01-01
    • 2013-07-22
    • 2020-02-10
    • 2012-10-01
    • 2014-11-05
    • 2017-10-29
    • 2011-11-13
    • 1970-01-01
    相关资源
    最近更新 更多