node.js - azure node.js 请求、响应 - 寻求一些澄清

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

我正在使用 Azure 移动服务-NodeJS 后端,在对其进行编程时,我总是面临这个疑问 - 让我使用下面的代码片段进行解释

//--------------------------------------------------------------------------
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文档时,我想我应该将我的疑问抛给专家社区以获得更清晰的解释。

非常感谢。

最佳答案

第一

在第二个return(buildAppUserEntityObj函数的inode)中,我相信您希望它是:

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

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

<小时/>

需要先return;,否则会继续执行下面的代码。

第二个

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

第三

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

关于node.js - azure node.js 请求、响应 - 寻求一些澄清,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27253088/

相关文章:

linux - 路径环境变量在注销并登录后不起作用

javascript - ReactJS 上 NPM 启动时出错

javascript - 如何使用观察者来检查div是否可见? (也许Azure服务器有问题?)

Node.js 和 Express - BadRequestError : request aborted

javascript - 'this' 在子类中未定义

node.js - 在 Express 中监听 res.sendFile 错误

python - 阻止QFileDialog中的文件操作,例如复制、查看、删除等

java - 访问 Blob 时 Azure Function 超时

node.js - Nodejs Express 捕获对服务器上公共(public)文件的调用

node.js - iisnode 在处理请求时遇到错误。 HTTP状态: 500