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. luogu3865 【模板】 ST表

    题目大意:给出一段序列,每次查询一段区间,求区间最大值. ST表:设原序列为A,定义F[i][k]为A[i][2k-1]的最大值.有递归式:F[i][k]=max(F[i][k-1], F[i+2k- ...

  2. ps -aux ,ps aux ,ps -ef 的区别

    Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...

  3. H3BPM子表的复制

    在做一个流程的时候,碰到了下面的表数据直接从上表中获取,并且为不可编辑状态,没有增加和删除行的按钮.一开始使用的是ComputationRule属性,但是有一项是日期空间,没有这个属性,不知道怎么处理 ...

  4. c.vim的安装和使用

    之前写了一些废话,今天写的稍微具体点. 在mac上给vim装个插件,,倒腾了半天 先要去找到c.vim.zip,自己百度下,官网就有的,我mac下下来,是直接解压的格式,而不是*.zip,也就是说不需 ...

  5. C - Queue at the School

    Problem description During the break the schoolchildren, boys and girls, formed a queue of n people ...

  6. 初探.net framework 下的异步多线程

    初探.net framework 下的异步多线程 目录 1.多线程的出现条件 2.Thread和ThreadPool的相关Api及用法 3.Task和Parallel的相关Api及用法 4.Async ...

  7. LUA 创建文件和文件夹

    创建文件: os.execute('mkdir e:\\aa') 创建文件夹: os.execute("cd.>e:\\wang.ini")

  8. 4.Projects and Scenes介绍

    1.Project 一个项目是由一系列的文件(如图片.音频.几何).场景以及vzp文件组成.这些文件被导入到项目对应的文件夹中.项目外部资源在场景中被使用后,会导入项目中,除非该资源被标记为外部引用. ...

  9. json属性(Jackson)

    Jackson相关:使用Jackson相关的注解时一定要注意自己定义的属性命名是否规范. 命名不规范时会失去效果.(例如Ename ,Eage 为不规范命名.“nameE”,“ageE”为规范命名). ...

  10. hdu3488 / hdu3435 / hdu1853 最小费用最大流 圈 拆点

    题目大意: 在一个有向图中,求经过所有点的最小圈. 思路: (如果是用二分图的完美匹配来做,那么直接上模版就好了).http://www.cnblogs.com/Potato-lover/p/3991 ...