简体   繁体   English

Expressjs路由请求和响应是异步的吗?

[英]Expressjs routes request & responses are async?

I have that doubt, really is not a problem but i want to know if my actual structure to handle requests and responses is non blocking. 我怀疑,真的不是问题,但我想知道我处理请求和响应的实际结构是否是非阻塞的。

Some of my code looks like this: 我的一些代码如下所示:

get: (req, res) ->
    permission = req.user.username

    if not permission
      return res.json new Unauthorized("#{req.user.username} no tiene permisos")

    Client.find()
      .exec (err, clients) ->
        if err then res.json new Internal(err.message, err.stack)
        if not clients then res.json new NotFound('No encontrado')

        res.json new Ok(clients, 'OK')

And of course is handled in a router archive, but i don't exactly know when my code is async and when it's not. 当然是在路由器存档中处理,但我不知道我的代码何时是异步的,什么时候不是。

So, can someone explain me if this is an async non blocking code? 那么,如果这是异步非阻塞代码,有人可以解释一下吗? If it's not, please, some bit of documentation or libraries? 如果不是,请,一些文档或库? I tryed to use promisejs but it throws me some errors in the execution (i think that's because im pretty noob). 我尝试使用promisejs,但它在执行中抛出了一些错误(我认为这是因为我很漂亮)。

That's all, thanks in advance! 这就是全部,在此先感谢!

Mongoose's exec() is an async function in your code. Mongoose的exec()是代码中的异步函数。 As a rule of thumb, expensive operations are asynchronous, while cheap operations are not*. 根据经验,昂贵的操作是异步的,而廉价的操作不是*。 Expensive means an operation that takes some ms, such as reading a remote file, accessing a (possibly) remote database or even reading a local file. 昂贵意味着需要几毫秒的操作,例如读取远程文件,访问(可能)远程数据库甚至读取本地文件。 Some examples of expensive, async operations: 昂贵的异步操作的一些示例:

  • request : load a external website. 请求 :加载外部网站。
  • fs : reading files from your system. fs :从系统中读取文件。 Most of the functions are asynchronous, but as reading a file is not prohibitively expensive, they also have a synchronous version. 大多数函数都是异步的,但由于读取文件并不昂贵,因此它们也具有同步版本。
  • mongoose : as @Molda explained, your mongoose call is asynchronous. 猫鼬 :如@Molda解释,你的猫鼬调用是异步的。 Most of the mongoose operations are. 大多数猫鼬行动都是。

Furthermore, asynchronous functions are characterized by them breaking the flow of the file, so they normally include a callback where the execution flow continues. 此外,异步函数的特征在于它们打破了文件的流,因此它们通常包括执行流继续的回调。 In node these callbacks accept two or more arguments as a convention, being the first argument an error and the other ones the data being retrieved. 在节点中,这些回调接受两个或多个参数作为约定,第一个参数是错误,另一个参数是检索的数据。 Examples for the previous ones: 以前的示例:

request('httsp://google.com/', function(err, response, body){
  console.log(body);   // print the website's html on the terminal
});

fs.readFile('/config.js', function(err, message){
  console.log(message);   // print the contents of config.js on the terminal
});

User.find({ id: 25 }, function(err, user){
  console.log(user);  // print user's 25 data on the terminal
});

Edit: as an alternative you could simplify your code by one line by including the callback in find() , making this function async: 编辑:作为替代方案,您可以通过在find()包含回调来简化代码一行,使此函数异步:

Client.find {}, (err, clients) ->
  if err then res.json new Internal(err.message, err.stack)
  if not clients then res.json new NotFound('No encontrado')

  res.json new Ok(clients, 'OK')

*please note that expensive and cheap are highly relative. *请注意, 昂贵廉价是高度相关的。 For instance, parsing a huge file in JSON can be really expensive and they are synchronous, while doing a small file read is relatively cheap. 例如, 使用JSON解析一个巨大的文件可能非常昂贵并且它们是同步的,而执行小文件读取则相对便宜。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM