High availability apps require that no distinction be made between local and remote services. Attached resources should be accessed by environment variables, and in doing so allow you to swap out one attached resource for another.

In this lesson we will setup a reverse proxy that directs image path requests and routes them through a defined server URL. By doing so, we decouple server requests for images which allows for easy switching from locally-served image assets to a CDN by simply updating an environment variable.

Install:
yarn add express-http-proxy
require('dotenv').config(); 

const path = require('path');
const express = require('express');
// require the lib
const proxy = require('express-http-proxy');
// read base image url from the env variable
const baseImageUrl = process.env.BASE_IMAGE_URL;
// Setup proxy image url by cond
const proxyBaseImageUrl = baseImageUrl // if the basImage url was given
? proxy(baseImageUrl, {
proxyReqPathResolver: function (req) { // set image url from a remote server
const newPath = baseImageUrl + req.path;
console.log(newPath);
return newPath;
}
})
: express.static(path.join(__dirname, 'public/images')); // fallback to local
const app = express(); app.use('/images', proxyBaseImageUrl); app.listen(8080);

Create .env file:

BASE_IMAGE_URL= https://goo.xxxxxxxxxxxxxxxxxx

[Node.js] Proxy Requests for Local and Remote Service Parity的更多相关文章

  1. [译]How to Install Node.js on Ubuntu 14.04 如何在ubuntu14.04上安装node.js

    原文链接为 http://www.hostingadvice.com/how-to/install-nodejs-ubuntu-14-04/ 由作者Jacob Nicholson 发表于October ...

  2. Four Node.js Gotchas that Operations Teams Should Know about

    There is no doubt that Node.js is one of the fastest growing platforms today. It can be found at sta ...

  3. ES6 学习笔记 (2)-- Liunx环境安装Node.js 与 搭建 Node.js 开发环境

    笔记参考来源:廖雪峰老师的javascript全栈教程 一.安装Node.js 目前Node.js的最新版本是6.2.x.首先,从Node.js官网下载对应平台的安装程序. 1.下载 选择对应的Liu ...

  4. Node.js NPM Tutorial: Create, Publish, Extend & Manage

    A module in Node.js is a logical encapsulation of code in a single unit. It's always a good programm ...

  5. [Node] Setup an Nginx Proxy for a Node.js App

    Learn how to setup an Nginx proxy server that sits in front of a Node.js app. You can use a proxy to ...

  6. Node.js Web 开发框架大全《中间件篇》

    这篇文章与大家分享优秀的 Node.js 中间件模块.Node 是一个服务器端 JavaScript 解释器,它将改变服务器应该如何工作的概念.它的目标是帮助程序员构建高度可伸缩的应用程序,编写能够处 ...

  7. 阿里云 CentOS7.2 配置FTP+Node.js环境

    本人小白,写下这篇博客意在记录踩过的坑,大神请绕道~ 准备工作 安装自己喜欢的连接软件(一般是putty或者xshell),本人选择的是xshell,软件如图 : 通过软件中的ssh连接连接上已经购买 ...

  8. Node.js npm 详解

    一.npm简介 安装npm请阅读我之前的文章Hello Node中npm安装那一部分,不过只介绍了linux平台,如果是其它平台,有前辈写了更加详细的介绍. npm的全称:Node Package M ...

  9. A chatroom for all! Part 1 - Introduction to Node.js(转发)

    项目组用到了 Node.js,发现下面这篇文章不错.转发一下.原文地址:<原文>. ------------------------------------------- A chatro ...

随机推荐

  1. 理解和配置 Linux 下的 OOM Killer【转】

    本文转载自:http://www.vpsee.com/2013/10/how-to-configure-the-linux-oom-killer/ 最近有位 VPS 客户抱怨 MySQL 无缘无故挂掉 ...

  2. 当使用Spring MVC @Valid对输入框进行验证的时候,可能会遇到以下的异常:Neither BindingResult nor plain target object for bean name ‘mybean’ available as request attribute

    转自:https://www.cnblogs.com/wenhulu/p/5555457.html 当使用Spring MVC @Valid对输入框进行验证的时候,可能会遇到以下的异常: Neithe ...

  3. [牛客挑战赛 30D] 小A的昆特牌 解题报告 (组合数学)

    interlinkage: https://ac.nowcoder.com/acm/contest/375/D description: solution: 我们枚举步兵的数量$x$,还剩下$S-x$ ...

  4. 微信小程序蓝牙连接小票打印机

    1.连接蓝牙 (第一次发表博客)   第一步打开蓝牙并搜索附近打印机设备// startSearch: function() { var that = this wx.openBluetoothAda ...

  5. 【算法】Quick Select

    针对问题 找到一对无序的数中第  K  大,或者第 K 小的元素,返回该元素的值或者它的 index(index 的情况比较适合这堆数每个都独一无二的情况,不然可能会有多个答案). 关键思想 拿一个数 ...

  6. 如何用jquery+ajax写省市区的三级联动?(封装和不封装两种方式)-----2017-05-14

    首先,要实现如下图效果, 1.要理清思路: 先做出三个下拉菜单----根据第一个下拉菜单的value值获取第二个下拉列表的内容,第三个同理. 2.用到的数据库表:Chinastates表 规律:根据国 ...

  7. map使用

    // map使用 1 #include <iostream> #include "insertVal.h" #include "sort.h" us ...

  8. 介绍一个简单的Parser

    我们已经学习了怎样创建一个简单的Monad, MaybeMonad, 并且知道了它如何通过在 Bind函数里封装处理空值的逻辑来移除样板式代码. 正如之前所说的,我们可以在Bind函数中封装更复杂的逻 ...

  9. 修改织梦plus目录名

    1.修改plus目录名 修改inlclude文件夹下common.inc.php 140行 //插件目录,这个目录是用于存放计数器.投票.评论等程序的必要动态程序 $cfg_plus_dir = $c ...

  10. 常用MySql命令列选

    常用MySql命令列选 命令 参数 含义 alter 数据库,表 修改数据库或表 backup 表 备份表 \c   取消输入 create 数据库,表 创建数据库或表 delete 表和行的表达式 ...