简体   繁体   English

用于与不同API Web服务进行交互的Nodejs Facade模式

[英]Nodejs Facade pattern for interact with different API web services

I am working with a NodeJs App and I have to call to a online web service in the logic part. 我正在使用NodeJs应用程序,因此必须在逻辑部分中调用在线Web服务。

The problem is that if that web service is taken down, the whole system stop working. 问题是,如果该Web服务被关闭,则整个系统将停止工作。

To deal with this I have though in use a Facade pattern, adding another web service with the same functionality, and one offline file with similar information (but not as good as the web service's). 为了解决这个问题,我使用了Facade模式,添加了另一个具有相同功能的Web服务以及一个具有类似信息的脱机文件(但不如Web服务的好)。

The idea is call the Facade (javaScript file?) from the logic part. 这个想法从逻辑部分称为Facade(javaScript文件?)。 It has to choose first the primary web service and call it, if it is down, go for the second one, and if it is also down, call the offline data file. 它必须首先选择主要的Web服务并调用它,如果它关闭了,则选择第二个,如果它也关闭了,则调用脱机数据文件。

Any idea about how to stucture this on NodeJS? 关于如何在NodeJS上进行构造的任何想法?

This is a possible solution 这是一个可能的解决方案

 // Simple GET request example: $http({ method: 'GET', url: '/someUrl1' }).then(function (response) { // this callback will be called asynchronously // when the response is available }, function (response) { // called asynchronously if an error occurs // or server returns response with an error status. // In case it fails $http({ method: 'GET', url: '/someUrl2' }).then(function (response) { // this callback will be called asynchronously // when the response is available }, function (response) { // called asynchronously if an error occurs // or server returns response with an error status. // If it fails again, use JSON file here }); }); 

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

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