アプリの概要
Express.js 4 モジュールと node.js コア http モジュールで実装された node.js サーバー アプリケーションがあります。大まかに言うと、アプリは受信クライアントの http メッセージを受け取り、(http モジュールを使用して) 他の外部 API に対してさまざまな http 呼び出しを行い、最後に、前述のさまざまな外部 http API 応答からの応答に基づいてクライアントに応答を返します。
問題
私の問題は、着信クライアントの http 要求がクライアントによって終了された場合 (たとえば、クライアントが要求をキャンセルしたい場合)、node.js アプリが前述のさまざまな外部 http API 呼び出しを続行することです。このような場合、node.js アプリの残りの部分に通知して、外部 API へのさまざまな送信 http 要求を終了する方法を見つけることができないようです。
クライアントがリクエストを終了すると、エクスプレス アプリ (つまり、エクスプレス http サーバー) は、私がリッスンしている「クローズ」イベントを受け取ります。私のコードの「閉じる」イベント リスナーは、このイベントをキャッチします。ただし、コードによって作成された「ダウンストリーム」または「後続の」httpリクエストに終了するよう通知する方法を理解できないようです。
私の目標
クライアントがサービスへの着信要求を終了したときに終了するように、単一のクライアント着信要求に関連付けられている外部 API へのすべての発信 http 要求に通知するにはどうすればよいですか?
問題をより明確に説明するために、いくつかのインライン コード コメントを付けて、以下の node.js アプリの簡易バージョンを提供しました。どんな助けや洞察も非常に高く評価されます。ありがとう!
追加情報
Apigee swagger-tools ミドルウェアを使用して API ルーティングを行っています。
私はいくつかの回答済みの質問を見つけましたが、それらは似ていますが、私の質問に直接は当てはまりません:
Express/Node.js と Angular でキャンセルされたリクエストを処理する
一番、
クリス
test-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();
});
})
test-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);
});
})
}