node工具之http-proxy-middleware
简介
一个轻松的配置代理服务器的中间件,让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的更多相关文章
- node工具是是什么东西
Node到底是个啥? Node是一个服务器端JavaScript解释器,可是真的以为JavaScript不错的同学学习Node就能轻松拿下,那么你就错了,总结:水深不深我还不知道,不过确实不浅 最近写 ...
- [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工具之nodemon
nodemon nodemon是一种工具,可以自动检测到目录中的文件更改时通过重新启动应用程序来调试基于node.js的应用程序. 安装 npm install -g nodemon //或 npm ...
- node工具之node-ip
node-ip node.js用来获取id地址的工具 use var ip = require('ip'); ip.address() // my ip address ip.isEqual('::1 ...
- node工具--express
//使用supervisor Connect是基于HTTP米快创建的:Express则是基于Connect上创建的: 绝大多数web服务器和浏览器之间的任务是通过url和method完成的,两者的组 ...
- node工具--connect
HTTP构建一个网站: var http = require('http'); var fs = require('fs'); var server = http.createServer(funct ...
- node工具之pm2
pm2 PM2是带有内置负载平衡器的Node.js应用程序的生产过程管理器.它使您可以使应用程序永远保持活动状态,无需停机即可重新加载它们,并简化常见的系统管理任务. 安装 npm install p ...
- Connect is a middleware layer for Node.js
Connect is a middleware layer for Node.js http://senchalabs.github.com/connect Connect Connect is ...
- NTVS:把Visual Studio变成Node.js IDE 的工具
NTVS(Node.js Tools for Visual Studio) 运行于VS2012或者VS2013.一些node.js的爱好者已经从PTVS(Python Tools for Visual ...
随机推荐
- [题解] [CQOI2007] 余数求和
题面 题解 考虑到这个等式\(a\bmod b = a - b * \lfloor\frac{a}{b}\rfloor\) 所以我们可以得到: \[ \begin{aligned} ans & ...
- 「BZOJ4242」水壶
题目链接 戳我 \(Solution\) 我们看到这题之后发现这题不是\(n^2\)把边弄出来后就跟货车运输差不多了,但是看了数据后发现\(n^2\)条边建不出来啊,这里就不详细的讲\(kruskal ...
- python3笔记二十:时间操作time
一:学习内容 time时间戳 time元组 time字符串 time时间转换图解 二:time 需要引入:import time 1.概念 UTC(世界协调时间):格林尼治天文时间,世界标准时间,在中 ...
- java实现几种常用排序:选择排序
一.选择排序介绍 选择排序,顾名思义就是用逐个选择的方式来进行排序,逐个选择出数组中的最大(或最小)的元素,直到选择至最后一个元素.此时数组完成了排序. 二.选择排序原理分析 三.选择排序代码实现 / ...
- hotspot 线程状态
- 关于Android Studio加载.so文件问题
在main文件下创建jniLibs文件,然后把.so文件copy过去,然后在 app的 build.gradle里面添加如下代码 sourceSets { main { jniLibs.srcDirs ...
- ConstraintLayout的简单介绍和使用
ConstraintLayout是Android Studio 2.2中主要的新增功能之一,也是Google在去年的I/O大会上重点宣传的一个功能.我们都知道,在传统的Android开发当中,界面基本 ...
- Statement、 PreparedStatement 、CallableStatement 区别和联系
Statement. PreparedStatement .CallableStatement 区别和联系 1. Statement.PreparedStatement和CallableStateme ...
- rocketmq的windows版客户端的启动步骤
一.下载RocketMQ首先,我们去下载RocketMQ,RocketMQ 是一个开源的东西,可以去github上面下载,地址是:https://github.com/alibaba/RocketMQ ...
- linux常用命令(8)cat命令
cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. 1 命令格式:cat [选项] [文件]. ...