search

Home  >  Q&A  >  body text

node.js - nodejs的http.get超时问题以及“装饰器”问题

我用nodejs 进行http.get请求时,发现遇到无效网址,往往会出现卡死,经过google发现http.get好像不能设置超时,google到设置超时也有其他方法,可是我一个新手,对于那些代码看得不太懂,希望大家同通俗易懂说说这个的问题

关于"装饰器"是由python中引申出来的,是另一个问题,比如计算一个函数执行时间,在python中写一个caculateTime装饰器,很容易做到这一点,而且复用性也很高。现在我在nodejs想要实现类似的功能,nodejs有没有类似的语法特性或解决方案?

伊谢尔伦伊谢尔伦2829 days ago779

reply all(2)I'll reply

  • 黄舟

    黄舟2017-04-17 11:11:34

    @decorator
    def func():
      pass
    

    is equivalent to

    def func():
      pass
    func = decorator(func)
    

    So in JavaScript you can write:

    var func = function(){};
    func = decorator(func);
    

    reply
    0
  • PHPz

    PHPz2017-04-17 11:11:34

    process.hrtime() is used to calculate function execution time.

    As for the first question, you need to listen to socket events, then handle socket.setTimeout() to set the timeout, and listen to tiemout to execute the timeout callback.

    Http request is based on socket, please check the relevant documentation.

    reply
    0
  • Cancelreply