传入的客户端http请求终止时终止传出的http请求

时间:2016-08-24 17:56:06

标签: node.js http events express swagger-tools

应用概述

我有一个使用express.js 4模块和node.js核心http模块实现的node.js服务器应用程序。在较高级别,应用程序接收传入的客户端http消息,对其他外部API进行各种http调用(使用http模块),最后根据上述各种外部http API响应的响应向客户端发回响应。 / p>

问题

我的问题是,当客户端终止传入的客户端http请求时(例如,当客户端想要取消他们的请求时),我的node.js应用程序继续进行上述各种外部http API调用。在这种情况下,我似乎无法找到一种方法向我的node.js应用程序的其余部分发出信号,以终止其对外部API的各种传出http请求。

当客户端终止他们的请求时,快递应用程序(即快速http服务器)会收到"关闭"我正在听的事件。 "关闭"我的代码中的事件监听器捕获了这个事件;但是,我似乎无法弄清楚如何向"下游发出信号"或"后续"我的代码发出的http请求终止。

我的目标

如何向外部API发送所有传出的http请求,这些请求与单个客户端传入请求相关联,以便在客户端终止其对我的服务的传入请求时终止?

我在下面提供了我的node.js应用程序的简化版本,其中包含一些内联代码注释,以帮助更清楚地说明我的问题。非常感谢任何帮助或见解。谢谢!

其他信息

我使用Apigee swagger-tools中间件来进行api路由。

我发现了一些与我的问题类似但不太直接适用的问题:

Handling cancelled request with Express/Node.js and Angular

How to detect user cancels request

最佳,

克里斯

测试app.js

// test-app.js
"use strict";

var swaggerTools = require("swagger-tools");
var app = require("express")();

// swaggerRouter configuration
// sends incoming http messages to test-controller.js
var options = {
    controllers: './controllers'
};

// The Swagger document (require it, build it programmatically, fetch it from a URL, ...)
// describes the API specification
var apiSpec = require('./test-swagger.json');

// Initialize the Swagger middleware
swaggerTools.initializeMiddleware(apiSpec, function (middleware) {
    "use strict"

    // Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
    app.use(middleware.swaggerMetadata());

    // Validate Swagger requests/responses based on test-swagger.json API specification
    app.use(middleware.swaggerValidator());

    // Route validated requests to appropriate controller, test-controller.js
    app.use(middleware.swaggerRouter(options));
});

// Run http server on port 8080
app.listen(8080, function () {
    "use strict";
    console.log("Server running on port %d", this.address().port);
})
    .on("connection", function (socket) {
        console.log("a new connection was made by an incoming client request.");
        socket.on("close", function () {
            console.log("socket connection was closed by client");
            // somehow signal to the rest of my node.js app to terminate any
            // http requests being made to external APIs, e.g. twitter api
            socket.destroy();
        });
    })

测试controller.js

//test-controller.js
"use strict";
var http = require("https");

// only one function currently, consequently, all incoming http requests are
// routed to this function, i.e. "compile"
module.exports = {
    compile: compile
};

function compile(req, res, next) {
    var options = {
        "method": "GET",
        "hostname": "api.twitter.com",
        "path": "/1.1/statuses/mentions_timeline.json?count=2&since_id=14927799",
        "headers": {"accept": "application/json"}
    };
    // how can i terminate this request when the http.server in test-app.js receives the "close" event?
    http.request(options)
        .on("response", function(response) {
            var apiResponse = [];
            response.on("data", function (chunk) {
                apiResponse.push(chunk);
            });
            response.on("end", function () {
                apiResponse = Buffer.concat(apiResponse).toString();
                res.status(response.statusCode).set(response.headers).send(apiResponse);
            });
        })
}

1 个答案:

答案 0 :(得分:1)

在测试控制器的编译方法中,您应该能够执行以下操作:

$forum->topics->replies->count()