简介

一个轻松的配置代理服务器的中间件,让Node.js代理变得简单

url路径

         foo://example.com:8042/over/there?name=ferret#nose
\_/ \______________/\_________/ \_________/ \__/
| | | | |
scheme authority path query fragment

基本使用

var express = require('express');
var proxy = require('http-proxy-middleware'); var app = express(); app.use(
'/api',
proxy({ target: 'http://www.example.org', changeOrigin: true })
);
app.listen(3000);

两种形式

var apiProxy = proxy('/api', { target: 'http://www.example.org' });
//同样效果
var apiProxy = proxy('http://www.example.org/api');

配置

var options = {
target: 'http://www.example.org', // 目标
changeOrigin: true, // 虚拟站点必须
ws: true, // 代理websocket
pathRewrite: {
'^/api/old-path': '/api/new-path', // 重写路径
},
router: {
// when request.headers.host == 'dev.localhost:3000',
// override target 'http://www.example.org' to 'http://localhost:8000'
'dev.localhost:3000': 'http://localhost:8000'
}
};

实际使用

const express = require("express");
const next = require("next");
const dev = process.env.NODE_ENV !== "production"; //判断是否是开发环境
const app = next({ dev });
const handle = app.getRequestHandler();
const compression = require("compression");
const port = parseInt(process.env.PORT, 10) || 6776;
const proxy = require("http-proxy-middleware"); const proxyOption = {
target: "http://127.0.0.1:6688",
pathRewrite: {
"^/api/": "/" // 重写请求,api/解析为/
},
changeOrigoin: true
}; app
.prepare()
.then(() => {
const server = express(); if (dev) {
server.use("/api/*", proxy(proxyOption));
} if (!dev) {
server.use(compression()); //gzip
} server.get("/", (req, res) => app.render(req, res, "/home"));
server.get("/home", (req, res) => app.render(req, res, "/home"));
server.get("/books", (req, res) => app.render(req, res, "/books"));
server.get("/articles", (req, res) => app.render(req, res, "/articles"));
server.get("/login", (req, res) => app.render(req, res, "/login"));
server.get("/markdown", (req, res) => app.render(req, res, "/markdown"));
server.get("/books", (req, res) => app.render(req, res, "/books"));
server.get("/write", (req, res) => app.render(req, res, "/write"));
server.get("/book/:currentBookId", (req, res) =>
app.render(req, res, "/book/[currentBookId]", { currentBookId: req.params.currentBookId })
);
server.get("/article/:curArticleId", (req, res) =>
app.render(req, res, "/article/[curArticleId]", { curArticleId: req.params.curArticleId })
);
server.all("*", (req, res) => handle(req, res)); server.listen(port, err => {
if (err) throw err;
else console.log(`http start at ===> http://localhost:${port}`);
});
})
.catch(ex => {
console.error(ex.stack);
process.exit(1);
});

doc

node工具之http-proxy-middleware的更多相关文章

  1. node工具是是什么东西

    Node到底是个啥? Node是一个服务器端JavaScript解释器,可是真的以为JavaScript不错的同学学习Node就能轻松拿下,那么你就错了,总结:水深不深我还不知道,不过确实不浅 最近写 ...

  2. [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 ...

  3. node工具之nodemon

    nodemon nodemon是一种工具,可以自动检测到目录中的文件更改时通过重新启动应用程序来调试基于node.js的应用程序. 安装 npm install -g nodemon //或 npm ...

  4. node工具之node-ip

    node-ip node.js用来获取id地址的工具 use var ip = require('ip'); ip.address() // my ip address ip.isEqual('::1 ...

  5. node工具--express

    //使用supervisor  Connect是基于HTTP米快创建的:Express则是基于Connect上创建的: 绝大多数web服务器和浏览器之间的任务是通过url和method完成的,两者的组 ...

  6. node工具--connect

    HTTP构建一个网站: var http = require('http'); var fs = require('fs'); var server = http.createServer(funct ...

  7. node工具之pm2

    pm2 PM2是带有内置负载平衡器的Node.js应用程序的生产过程管理器.它使您可以使应用程序永远保持活动状态,无需停机即可重新加载它们,并简化常见的系统管理任务. 安装 npm install p ...

  8. Connect is a middleware layer for Node.js

     Connect is a middleware layer for Node.js   http://senchalabs.github.com/connect Connect Connect is ...

  9. NTVS:把Visual Studio变成Node.js IDE 的工具

    NTVS(Node.js Tools for Visual Studio) 运行于VS2012或者VS2013.一些node.js的爱好者已经从PTVS(Python Tools for Visual ...

随机推荐

  1. Vue使用Axios实现http请求以及解决跨域问题

    Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中.Axios的中文文档以及github地址如下: 中文:https://www.kancloud.cn/y ...

  2. TCP被动打开 之 第一次握手-接收SYN

    假定客户端执行主动打开,服务器执行被动打开,客户端发送syn包到服务器,服务器接收该包,进行建立连接请求的相关处理,即第一次握手:本文主要分析第一次握手中被动打开端的处理流程,主动打开端的处理请查阅本 ...

  3. 20165213 Exp7 网络欺诈防范

    Exp7 网络欺诈防范 一. 实践内容 简单应用SET工具建立冒名网站 1.首先使用sudo vi /etc/apache2/ports.conf 进行查看listen的端口号,若不是80改为80. ...

  4. vxWorks下常用的几种延时方法

         在应用编程的时候,通常会碰到需要一个任务在特定的延时之后执行一个指定的动作,如等待外设以确保数据可靠,控制扬声器发声时间以及串口通信超时重发等.这就需要利用定时器机制来计量特定长度的时间段. ...

  5. 文字和符号组合成图 Banner

    springboot 启动 logo.... 文字符号组合成的图. <<< | /\\\ /--\\\ / \\\\ <*| /________\\\\ | ___ | | | ...

  6. DP----鬼畜的数字三角形

    数字三角形 1   洛谷   P1216  数字金字塔 我们可以用 f [ i ] [ j ] 表示从(1,1)出发,到达(i,j)的最大权值和. (i , j)可以由 正上(i - 1 , j)或者 ...

  7. anroid学习笔记(1)

    大概是2个月前,报名了慕课的android就业班课程. 算是补全了当初博客分类的最初设计. 安卓和前端比较: 1,java在安卓开发中的作用,现在我的认识是和JavaScript在前端web开发中有很 ...

  8. leetcode378 有序矩阵中第k小的元素

    排序后取数组第k个元素,遍历需要n^2的复杂度,查找插入logn,时间复杂度O(n^2logn).方法很笨,完全就是STL过于牛x运行通过的. class Solution { public: int ...

  9. OOP、DI、IOC的关系

    此随笔的重点在“Demo分析”一章,以代码的分阶段变化讲述了DI,DIP,IOC的演变,写在前面文字均为铺垫. 希望各位园友拍砖,促使流浪者的进步,现在有很多问题想讨论,即以此文寻找志同道合的园友,另 ...

  10. SqlServer索引的原理与应用(转载)

    SqlServer索引的原理与应用 索引的概念 索引的用途:我们对数据查询及处理速度已成为衡量应用系统成败的标准,而采用索引来加快数据处理速度通常是最普遍采用的优化方法. 索引是什么:数据库中的索引类 ...