[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 ...
随机推荐
- sql server drop login failed
https://stackoverflow.com/questions/37275/sql-query-for-logins https://www.mssqltips.com/sqlserverti ...
- Gym-100935I Farm 计算几何 圆和矩形面积交
题面 题意:就是给你一个圆,和你一个矩形,求面积并,且 保证是一种情况:三角剖分后 一个点在圆内 两个在圆外 题解:可以直接上圆与凸多边形交的板子,也可以由这题实际情况,面积等于扇形减两个三角形 #i ...
- Entity Framework 的懒加载、预先加载、显示加载
1.新建两个实体,一个班级有多个学生 public class Student { public int StudentId { get; set; } public string StudentNa ...
- 集成Bmob推送
Write By lz: 转发请注明原文地址: http://www.cnblogs.com/lizhilin2016/p/6952217.html Lz 寄语: Bmob 奇葩推送, 大坑, 想要 ...
- RN配置
Write by lz: 详细参考官方网址: http://reactnative.cn/docs/0.43/getting-started.html#content 若是无法安装 Chocolate ...
- 几个方便编程的C++特性
前言: C++11的自动化特性给编程提供了不少方便,同时也给调试增加了很多负担,至于取舍看程序员的风格和侧重而定. auto:自动类型推断 在C++11之前,auto关键字用来指定存储期.在新标准中, ...
- 杭电1003 Max Sum 【连续子序列求最大和】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目意思: 即给出一串数据,求连续的子序列的最大和 解题思路: 因为我们很容易想到用一个max ...
- 移动端的0.5px
最近写移动端页面写的比较多,边边基本上都是用的1px,视觉上也确实有点小粗,这不闲下来啦,具体的研究了下0.5px是怎么实现的,切记,这个效果只有在手机上才能看到效果的 利用了css3的缩放效果 &l ...
- (转) RabbitMQ学习之helloword(java)
http://blog.csdn.net/zhu_tianwei/article/details/40835555 amqp-client:http://www.rabbitmq.com/java-c ...
- jQuery删除元素
remove() - 删除被选元素(及其子元素) empty() - 从被选元素中删除子元素 $("#div1").remove();删除被选元素及其子元素. $("#d ...