dora-js / dora-plugin-proxy Goto Github PK
View Code? Open in Web Editor NEWProxy plugin for dora.
Proxy plugin for dora.
看到源码的rule里面有配置'access-control-allow-origin': '*'
可是访问接口的时候依然提示No 'Access-Control-Allow-Origin' header
提示这个是少安装什么吗?
"dependencies": {
"antd": "^2.5.0",
"classnames": "^2.2.5",
"dva": "^1.1.0",
"js-cookie": "^2.1.3",
"qs": "^6.2.0",
"react": "^15.1.0",
"react-dom": "^15.1.0",
"recharts": "^0.19.0"
},
"devDependencies": {
"atool-build": "^0.7.6",
"babel-eslint": "^6.0.4",
"babel-plugin-dev-expression": "^0.2.1",
"babel-plugin-dva-hmr": "^0.1.0",
"babel-plugin-import": "^1.0.1",
"babel-plugin-transform-runtime": "^6.9.0",
"babel-runtime": "^6.9.2",
"dora": "^0.4.3",
"dora-plugin-proxy": "^0.8.5",
"dora-plugin-webpack": "^0.8.1",
"dora-plugin-webpack-hmr": "^0.2.1",
"eslint": "^2.13.1",
"eslint-config-airbnb": "^9.0.1",
"eslint-plugin-import": "^1.8.1",
"eslint-plugin-jsx-a11y": "^1.4.2",
"eslint-plugin-react": "^5.1.1",
"glob": "^7.0.5",
"mockjs": "^1.0.1-beta3",
"redbox-react": "^1.2.10",
"robe-ajax": "^1.0.0",
"watchjs": "^0.0.0"
}
每次默认的代理端口都是8989能改成其他的吗?
'/a/*': 'http://b.com/b/'
那么代理 /a/xxx
到 http://b.com/b/a/xxx
'/a/(.*)': 'http://b.com/b/'
那么代理 /a/xxx
到 http://b.com/b/xxx
看了dora插件写法,dora-plugin-proxy源码和anyproxy文档,貌似没有类似fs.watch and restart node server这类方法。
现在的日志是:
> dora -p 8001 --plugins atool-build,proxy,hmr
dora: listened on 8001
proxy: load rule with proxy.config.js
Anyproxy rule: dora-plugin-proxy
Anyproxy rules initialize finished, have fun!
web interface is disabled
GUI interface started at : http://192.168.2.33:8002/
Http proxy started at 192.168.2.33:8989
received request to : localhost:8989/api/1.js
==>use local rules
received request to : localhost:8989/favicon.ico
==>will forward to real server by proxy
期望的日志:
> dora -p 8001 --plugins atool-build,proxy,hmr
proxy: load rule from proxy.config.js
proxy: listened on 8989
proxy: respond localhost:8989/api/1.js with local file
dora: listened on 8001
提醒var comments = require('./mocks/comment');
module.exports = Object.assign({} , comments, {
'GET /xxx': {}
})
0.6.1版本, 不支持多 mock 文件
proxy: proxy.config.js parse error
dora -p 8888 --plugins proxy
改完了 端口还是 8989 要怎么写才会改端口
在做前端开发时,前端的ajax请求通常不能直接访问后台接口,这个时候我们需要做一个反向代理,例如在apache中我们通常这样配置:
ProxyPass /live http://test.live:8080/live
ProxyPassReverse /live http://test.live:8080/live
这样就将所有url包含live的请求转发到 http://test.live:8080/live,那么在这里如何配置呢,能不能类似这样
'POST /live/': ' http://test.live:8080/live/',
'GET /live/': ' http://test.live:8080/live/',
但是这样实际上是不行的,需要对每个请求单独配置
'POST /live/login.do': ' http://test.live:8080/live/login.do'
现在抛弃了对openssl的依赖,改用node-forge来做证书生成,windows用户有福了
比如:https://github.com/dora-js/dora-plugin-proxy/blob/master/src/getProxyConfig.js#L37
这个地方,直接catch了,导致错误提示不完善,造成的问题就是:
proxy.config.js 里面必须使用'use strict' 才能使用 require的部分库,因为:
Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
建议将这段改成:
try {
cache = require(proxyFile);
} catch (e) {
log.error(`${configPath} parse error : ${e}`);
}
XMLHttpRequest cannot load http://localhost:8989/a.do. Request header field tokenId is not allowed by Access-Control-Allow-Headers in preflight response
我的项目http全局设置这都会传tokenid的header 但是调用dora的api 就报错,因为你们没有配置header
就是想问下 你们怎么配置header
├── output
│ ├── index.html
│ └── static
├── package.json
├── proxy.config.js
如上工程结构,我想要的结果是 当访问http://127.0.0.1:8989/ 时 是以output为根目录开始查找并代理,类似apache的 document root 目录设置
Problem:
module.exports = {
'/api': function(req, res) {
// Can't get headers here
console.log(req.headers);
};
}
always getting 504 error:
npm info retry fetch attempt 1 at 18:07:16
npm info attempt registry request try #1 at 18:07:16
npm http fetch GET https://registry.npmjs.org/dora-plugin-proxy/-/dora-plugin-proxy-0.5.3.tgz
npm http fetch 504 https://registry.npmjs.org/dora-plugin-proxy/-/dora-plugin-proxy-0.5.3.tgz
npm info retry will retry, error on last attempt: Error: server error 504
npm info attempt registry request try #2 at 18:08:16
npm http fetch GET https://registry.npmjs.org/dora-plugin-proxy/-/dora-plugin-proxy-0.5.3.tgz
everything other from npm.js installs normally without any 504 errors
failed to create cert dir ,please create one by yourself - \Users\Junz.anyproxy_certs\
proxy: load rule from proxy.config.js
(node:8332) DeprecationWarning: 'GLOBAL' is deprecated, use 'global'
temp certs cleared
events.js:160
throw er; // Unhandled 'error' event
^
Error: spawn D:\code\listen_WebApp\node_modules\dora-anyproxy\cert\gen-rootCA.cmd ENOENT
at exports._errnoException (util.js:1007:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:182:32)
at onErrorNT (internal/child_process.js:348:16)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
at Module.runMain (module.js:577:11)
at run (node.js:348:7)
at startup (node.js:140:9)
at node.js:463:3
完全搞不懂什么原因。。。node版本6.2.2 检查过openssl 命令可用
比如说我想把/static/*
的请求返回本地目录/content/
下同名的文件。
优先级低,需要先想清楚场景。
module.exports = {
'GET /api': function(req, res){
res.json(
{ "title": "中文" }
);
res.set("Content-Type", "application/json; charset=UTF-8");
},
"GET /api2": { "title": "中文2" },
};
res
json(data)
jsonp(data, name='callback')
set(key, value)
status(statusCode)
end(data)
mockjs(data)
如上
Middleware to proxy requests through a specified index page, useful for Single Page Applications that utilise the HTML5 History API.
例如
规则: '/api/sms/*': 'http://localhost:9000'
发送http://localhost:3000/api/sms/provider
9000端口接收/api/sms/provider全路径,即便改为'http://localhost:9000/也一样
怎么写local.js 中的例子 有例子吗?
"dora --plugins \"proxy?watchDirs=/mock/,browser-history,webpack?publicPath=/,webpack-hmr\"",
watchDirs 如何支持多个文件夹?
如果项目使用了 GraphQL,那么所有的请求都是往同一个 url post GraphQL 语句,现在的话只能通过自定义函数替换请求,感觉都要把整个服务器给实现出来了 😂
看了下文档 没有发现相关的配置项。麻烦了。
有大神解答下么,我们在开发基于阿里的ant-design时候,用了里面的代理插件 http://ant-tool.github.io/dora.html#proxyconfigjs 然后我们配置好后,跟踪了一下,我们前端项目(windows机器)上启动了代理,转发到另外一台windows上的tomcat7服务器,然后路径由/变成了\,导致请求失败
Then we can pipe to it (res).
const request = require('request');
request(url).pipe(res);
I see at Chrome Developer set-cookie in header, but it never set. I suppose problem is with host or some CROS problems.
my proxy config is quite simple:
module.exports = {
'/api/syncapi/(.*)': 'http://luxms-web.local/;/syncapi/1.5',
};
that's request headers:
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8,ru;q=0.6
Connection:keep-alive
Content-Length:48
content-type:application/x-www-form-urlencoded
Host:localhost:8989
Origin:http://localhost:8001
Referer:http://localhost:8001/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36
and that's response headers:
access-control-allow-origin:*
connection:keep-alive
content-type:application/json; charset=utf-8
date:Thu, 09 Jun 2016 10:19:00 GMT
elapsed-time:0.053905010223389
server:nginx/1.8.0
set-cookie:BI-User-Session=48f3c32a-1453-445d-be0c-7ac89cbb566d; expires=Fri, 10 Jun 2016 10:19:38 GMT; path=/;
Transfer-Encoding:chunked
x-nginx-lua-version:10003
can you help me with setting up proxy to process cookies?
请问该插件支持post请求转发吗?
联调和本地开发需要不同的 proxy 机制,比如本地走 mock,联调时 forward 到另一个接口。
proxy 支持 config 参数,可指定 proxy.config.js 的路径。
"scripts": {
"dev": "dora --plugins proxy",
"dev-test": "dora --plugins proxy?config=proxy.config.test.js"
}
@sorrycc 你好!
我用 dva-cli 搭建了一个工程,之前我都是自己 mock 数据的:
'GET /v1/security/Accounts/Self/' (req, res) {
setTimeout(() => {
res.json({
success: true,
userInfo: {
nickname: 'banana',
age: 25
}
});
}, 500);
}
现在我希望直接调用远程服务器的API,于是我按照文档配置好了Chrome的代理规则,同时把上面这段代码修改成了(并重新 npm start
):
'GET /v1/security/Accounts/Self/' : HOST + '/v1/security/Accounts/Self/',
这样修改以后,当我调用 /v1/security/Accounts/Self/
这个接口的时候,控制台有输出下面的内容:
proxy: GET http://win2012r2.eastasia.cloudapp.azure.com/v1/security/Accounts/Self matches GET /v1/security/Accounts/Self/, forward to http://win2012r2.eastasia.cloudapp.azure.com/v1/security/Accounts/Self/
看上去规则应该是生效了
但是实际上并没有请求远程服务器的API,而是本地的,所以 Chrome 下返回了404:
所以,请问我到底在哪个环节出了错误?应该如何修改呢?
基于 proxy.config.js
,扩展类型为 plainObject 的情况,支持 Mock.js。
使用方式如下:
var movie = {
'id|+1': 1,
'name': '@Name',
};
module.exports = {
'GET /movies': {data|5: [movie], success: true},
'GET /movie/:id': {data: movie, success: true},
}
请求 /movies
,返回:
{
"success": true,
"data": [
{
"id": 3,
"name": "Gary Wilson"
},
{
"id": 2,
"name": "Lisa Thomas"
},
{
"id": 2,
"name": "Ronald Lewis"
},
{
"id": 3,
"name": "Jason Robinson"
},
{
"id": 5,
"name": "Karen Lopez"
}
]
}
请求 /movies/3
,返回:
{
"success": true,
"data": {
"id": 3,
"name": "Melissa Moore"
}
}
Hello,
While playing with antd-init
result and trying to proxy the requests to static assets, the following rule was included in proxy.config.js file:
'/assets/*': function(req, res) {
var options = {
root: __dirname,
dotfiles: 'deny',
headers: {
'x-timestamp': Date.now(),
'x-sent': true
}
};
var fileName = req.params[0];
res.sendFile(fileName, options, function (err) {
if (err) {
console.log(err);
res.status(err.status).end();
}
else {
console.log('Sent:', fileName);
}
});
},
The README includes an example of using custom functions which refers to Express API.
The custom function listed above is using res.sendFile() method. However, a request to
/assets/images/favicon.png crashes the node engine with the following error:
> npm start
> @ start /files/dev/react/ant-demo
> dora -p 8001 --plugins "webpack,hmr,proxy,livereload?enableJs=false&injectHost=127.0.0.1,browser-history?index=/src/entries/index.html"
proxy: load rule from proxy.config.js
proxy: listened on 8989
livereload: listening on 35729
📦 538/538 build modules
webpack: bundle build is now finished.
proxy: GET /assets/images/favicon.png matches /assets/*, respond with custom function
Caught exception: TypeError: res.sendFile is not a function
at module.exports./assets/* (/files/dev/react/ant-demo/proxy.config.js:46:9)
at /files/dev/react/ant-demo/node_modules/dora-plugin-proxy/lib/rule.js:52:11
at batchMatch (/files/dev/react/ant-demo/node_modules/dora-plugin-proxy/lib/rule.js:174:22)
at Object.dealLocalResponse (/files/dev/react/ant-demo/node_modules/dora-plugin-proxy/lib/rule.js:37:14)
at dealWithLocalResponse (/files/dev/react/ant-demo/node_modules/dora-anyproxy/lib/requestHandler.js:94:18)
at routeReq (/files/dev/react/ant-demo/node_modules/dora-anyproxy/lib/requestHandler.js:86:13)
at /files/dev/react/ant-demo/node_modules/dora-anyproxy/node_modules/async/lib/async.js:607:21
at /files/dev/react/ant-demo/node_modules/dora-anyproxy/node_modules/async/lib/async.js:246:17
at iterate (/files/dev/react/ant-demo/node_modules/dora-anyproxy/node_modules/async/lib/async.js:146:13)
at /files/dev/react/ant-demo/node_modules/dora-anyproxy/node_modules/async/lib/async.js:157:25
AnyProxy is about to exit with code: 0
>
Only the following methods are provided for res
?
If yes, what option should I have for the purpose of serving static files directly from such setup, without proxy-ing to other http endpoints, such as an Express instance specifically made for this?
Thanks.
utils.js中的normalizeData为什么不支持二进制的数据呢?res.json 已经有针对json数据的处理了,这里可以考虑支持二进制的数据否。
end: function end(data) {
callback(_status, headers, normalizeData(data));
return this;
}
function normalizeData(data) {
switch (typeof data === 'undefined' ? 'undefined' : _typeof(data)) {
case 'string':
return data;
default:
return JSON.stringify(data);
}
}
`
'/custom-func/:action': function(req, res) {
res.type(path);
fs.readFile(path, function (err, data) {
if (err) {
res.status(404);
res.end("404");
return;
}
res.type(srcDir + pathName)
if (S(mime.lookup(path)).startsWith('image/')) {
res.end(data);
} else {
res.end(data.toString());
}
});
}
'GET /(.*\.do)': 'http://127.0.0.1:8080/BackGround/'
tomcat访问日志:
127.0.0.1 - - [03/Nov/2016:22:46:32 +0800] "GET /finance/getInvoiceByAdvance.do HTTP/1.1" 404 1011
'GET /finance/getInvoiceByAdvance.do': 'http://127.0.0.1:8080/BackGround/'
tomcat访问日志:
127.0.0.1 - - [03/Nov/2016:22:45:02 +0800] "GET /BackGround/finance/getInvoiceByAdvance.do HTTP/1.1" 200 2059
路由匹配规则是按照 express 的来么?
配置如下:
module.exports = {
'GET /dispatch':'./',
'GET /task':'./index.html',
'GET /answer':'./index.html',
'GET /reply':'./index.html',
'GET /complete':'./index.html'
};
但是请求http://localhost:8001/dispatch
,还是404 Not Found
。
用了react-router
,想让访问这些router的时候,依然返回首页。
跨域请求json格式数据的时候出现
Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response
如果是text/html就不会报错,
我现在的配置
'POST /api/portal/user/login': function(req, res) {
console.dir(req.header);
res.set("Access-Control-Allow-Headers","Origin, X-Requested-With, Content-Type, Accept,Content-Range, Content-Disposition, Content-Description,Set-Cookie,, Access-Control-Request-Method, Access-Control-Request-Headers,Authorization,Authentication");
res.set("Access-Control-Allow-Methods","GET,POST,PUT,DELETE,PATCH,HEAD,OPTIONS");
//res.set("Access-Control-Allow-Credentials", "true");
res.set("Access-Control-Expose-Headers","Set-Cookie,Authorization,Authentication");
setTimeout(function() {
res.json({
success: true,
entity: [
{
id: 1,
text: 'Learn antd',
isComplete: true,
},
{
id: 2,
text: 'Learn ant-tool',
},
{
id: 3,
text: 'Learn dora',
}
],
message:'',
type:'0',
errorType:'0',
errorcode:''
});
}, 500);
}.
添加类似配置:
module.exports = { 'GET /demo.png': './demo.png', };
图片不能正常显示,而是作为文件下载,下载后的文件也不能正常打开
https://github.com/huangshunping/dora-plugin-proxy.git
上面是我增加了对图片的支持,望采纳
@sorrycc
SwitchOmega里,https不能配置url通配符,只能配置域名通配符。
如果我想转发部分目录的请求,剩下一部分送到真实的服务器,就做不到。
比如我配置了:
'/g/h5-lib/nebulamng-fe/0.0.1/(.*)': 'http://localhost:8001/'
在switchOmega里设置 *.test.xxx.net ,这时候请求 a.test.xxx.net/hello.js 就会报错。
按AnyProxy的玩儿法,可以考虑这么做:匹配规则的,返回规则内容,不匹配的,释放到线上真实服务器即可。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.