使用 Express/Node.js 和 Angular 处理取消的请求 [英] Handling cancelled request with Express/Node.js and Angular

查看:35
本文介绍了使用 Express/Node.js 和 Angular 处理取消的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当客户端/浏览器取消挂起的 HTTP 请求时,Node with Express 似乎会继续处理该请求.对于密集请求,CPU 仍然忙于处理不必要的请求.

When a pending HTTP request is cancelled by a client/browser it seems that Node with Express continues to process the request. For intensive requests, the CPU is still being kept busy with unnecessary requests.

有没有办法让 Node.js/Express 杀死/停止这些请求取消的待处理请求?

Is there a way to ask Node.js/Express to kill/stop these pending requests that are requested to be cancelled?

鉴于 AngularJS 1.5 HTTP 请求很容易可取消 通过对 $http/$resource 对象调用 $cancelRequest().

It becomes particularly useful given that since AngularJS 1.5 HTTP request are easily cancellable by calling $cancelRequest() on $http/$resource objects.

当公开提供自动完成或搜索字段结果的 API 方法时,可能会发生此类取消:在字段中输入要自动完成或预先输入的内容时,可以取消之前的请求.

Such cancellations could occur when exposing an API method providing results for auto-completion or search fields: when typing in the field to be autocompleted or type-aheaded, previous request(s) can be cancelled.

全局 server.timeout 不能解决问题:1) 它是所有公开 API 方法的先验全局设置 2) 取消的请求中正在进行的处理不会被终止.

A global server.timeout does not solve the problem: 1) it is a priori a global setting for all exposed API methods 2) ongoing processing in the canceled request is not killed.

推荐答案

注入的 req 对象与侦听器 .on() 一起提供.

Injected reqobject is shipped with listeners .on().

侦听 close 事件允许在客户端关闭连接时进行处理(Angular 取消了请求,或者,例如,用户关闭了查询选项卡).

Listening to close event allows to handle when client close the connection (request cancelled by Angular or, e.g., user closed the querying tab).

这里有两个简单的例子,说明如何使用 close 事件来停止请求处理.

Here are 2 simple examples how to use the closeevent to stop request processing.

示例 1:可取消的同步块

var clientCancelledRequest = 'clientCancelledRequest';

function cancellableAPIMethodA(req, res, next) {
    var cancelRequest = false;

    req.on('close', function (err){
       cancelRequest = true;
    });

    var superLargeArray = [/* ... */];

    try {
        // Long processing loop
        superLargeArray.forEach(function (item) {
                if (cancelRequest) {
                    throw {type: clientCancelledRequest};
                }
                /* Work on item */
        });

        // Job done before client cancelled the request, send result to client
        res.send(/* results */);
    } catch (e) {
        // Re-throw (or call next(e)) on non-cancellation exception
        if (e.type !== clientCancelledRequest) {
            throw e;
        }
    }

    // Job done before client cancelled the request, send result to client
    res.send(/* results */);
}

示例 2:带有 promise 的可取消异步块(类似于reduce)

function cancellableAPIMethodA(req, res, next) {
    var cancelRequest = false;

    req.on('close', function (err){
       cancelRequest = true;
    });

    var superLargeArray = [/* ... */];

    var promise = Q.when();
    superLargeArray.forEach(function (item) {
            promise = promise.then(function() {
                if (cancelRequest) {
                    throw {type: clientCancelledRequest};
                } 
                /* Work on item */ 
            });
    });

    promise.then(function() {
        // Job done before client cancelled the request, send result to client
        res.send(/* results */);
    })
    .catch(function(err) {
        // Re-throw (or call next(err)) on non-cancellation exception
        if (err.type !== clientCancelledRequest) {
            throw err;
        }
    })
    .done();
}

这篇关于使用 Express/Node.js 和 Angular 处理取消的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆