[Node.js] Proxy Requests for Local and Remote Service Parity
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.
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的更多相关文章
- [译]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 ...
- 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 ...
- ES6 学习笔记 (2)-- Liunx环境安装Node.js 与 搭建 Node.js 开发环境
笔记参考来源:廖雪峰老师的javascript全栈教程 一.安装Node.js 目前Node.js的最新版本是6.2.x.首先,从Node.js官网下载对应平台的安装程序. 1.下载 选择对应的Liu ...
- 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 ...
- [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 ...
- Node.js Web 开发框架大全《中间件篇》
这篇文章与大家分享优秀的 Node.js 中间件模块.Node 是一个服务器端 JavaScript 解释器,它将改变服务器应该如何工作的概念.它的目标是帮助程序员构建高度可伸缩的应用程序,编写能够处 ...
- 阿里云 CentOS7.2 配置FTP+Node.js环境
本人小白,写下这篇博客意在记录踩过的坑,大神请绕道~ 准备工作 安装自己喜欢的连接软件(一般是putty或者xshell),本人选择的是xshell,软件如图 : 通过软件中的ssh连接连接上已经购买 ...
- Node.js npm 详解
一.npm简介 安装npm请阅读我之前的文章Hello Node中npm安装那一部分,不过只介绍了linux平台,如果是其它平台,有前辈写了更加详细的介绍. npm的全称:Node Package M ...
- A chatroom for all! Part 1 - Introduction to Node.js(转发)
项目组用到了 Node.js,发现下面这篇文章不错.转发一下.原文地址:<原文>. ------------------------------------------- A chatro ...
随机推荐
- pom.xml出现web.xml is missing and <failOnMissingWebXml> is set to true解决方案
提示信息应该能看懂.也就是缺少了web.xml文件,<failOnMissingWebXml>被设置成true了. 搜索了一下,Stack Overflow上的答案解决了问题,分享一下. ...
- php创建简单的列表页
php创建简单的列表页 样例 代码 <?php $userInfo[] = array( 'id'=>'1', 'username'=>'fry', 'sex'=>'nan', ...
- 杂项:DS(目录服务)
ylbtech-杂项:DS(目录服务) 1.返回顶部 1. DS(目录服务). 目录服务管理概述: 目录服务是扩展计算机系统中最重要的组件之一.虽然用户和管理通常不知道他们感兴趣对象的确切名称,但他们 ...
- Html+CSS基础之Html
注:本文摘自慕课网http://www.imooc.com HTML+CSS基础课程 本人有道笔记:http://note.youdao.com/noteshare?id=18fa811e9f9e ...
- 一步步教会你微信小程序的登录鉴权
前言 为了方便小程序应用使用微信登录态进行授权登录,微信小程序提供了登录授权的开放接口.乍一看文档,感觉文档上讲的非常有道理,但是实现起来又真的是摸不着头脑,不知道如何管理和维护登录态.本文就来手把手 ...
- 自定义typecho后台路径
如何自定义后台路径 Typecho 安装好后,默认的后台路径是 domain.com/admin/,为了提高安全性,我们允许以 domain.com/xxxx/ 的方式访问,其中 xxxx 是你自定义 ...
- (到8.1为止)Android版本名称与内容
版本名称 API 主要版本更新时间 Android 1.0 1 测试版本 Android 1.1 2 2008 年9月正式发布的Android第一版 Android 1.5 Cupcake(纸杯 ...
- eclipse离线安装pydev
首先,下载去http://pydev.org/下载Python的Eclipse插件PyDev. 目前的最新版是PyDev 2.7.1.zip,将压缩文件解压出来.得到features和plugins两 ...
- caffe特征提取/C++数据格式转换
Caffe生成的数据分为2种格式:Lmdb 和 Leveldb 它们都是键/值对(Key/Value Pair)嵌入式数据库管理系统编程库. 虽然lmdb的内存消耗是leveldb的1.1倍,但是lm ...
- java学习笔记5——String类常用方法
1.字符串长度计算: int i = String1.length(); 2.字符串比较:1) equals()和equalsIgnoreCase //比较两个字符串对象的实体是否相同,相同输出tru ...