zoukankan      html  css  js  c++  java
  • [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.

    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
  • 相关阅读:
    加密的PDF文件怎么查看
    SQL Server数据库中怎么加密数据
    虚拟磁盘也加密
    怎么用密码保护您的文件夹
    如何用cmd命令加密文件夹
    快压轻松创建加密压缩包
    GPS学习的一些链接
    【最新软件预告】邮件群发器Console版
    第六格很酷的滚动活动会员条
    邀请您加入移动开发专家联盟
  • 原文地址:https://www.cnblogs.com/Answer1215/p/8561876.html
Copyright © 2011-2022 走看看