通过Express.js反向代理调用Plot.ly Dash

时间:2019-03-04 12:17:31

标签: node.js express flask plotly-dash

我正在使用Node.js / Express.js反向代理来服务我的仪表板安装。 模板和javascript资源已正确加载,但api更新HTTP POST调用/_dash-update-componentWerkzeug/0.14.1 Python/3.6.7服务器返回 405不允许使用方法错误:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>405 Method Not Allowed</title>
<h1>Method Not Allowed</h1>
<p>The method is not allowed for the requested URL.</p>

我的反向代理实现很简单:

self.app.get('/_dash-component-suites/:component/:file', function (req, res) {
                var target_uri = self._options.proxy.dash.uri + req.url;
                proxy.web(req, res, {
                    //true/false, passes the absolute URL as the path (useful for proxying to proxies)
                    toProxy: false,
                    //true/false, Default: true - specify whether you want to prepend the target's path to the proxy path
                    prependPath: true,
                    //  url string to be parsed with the url module
                    target: target_uri,
                    //  true/false, Default: false - changes the origin of the host header to the target URL
                    changeOrigin: true,
                    //  true/false, Default: false - specify whether you want to ignore the proxy path of the incoming request 
                    ignorePath: true
                });
            });//dash
            self.app.get('/_dash-layout', function (req, res) {
                var target_uri = self._options.proxy.dash.uri + req.url;
                proxy.web(req, res, {
                    //true/false, passes the absolute URL as the path (useful for proxying to proxies)
                    toProxy: false,
                    //true/false, Default: true - specify whether you want to prepend the target's path to the proxy path
                    prependPath: true,
                    //  url string to be parsed with the url module
                    target: target_uri,
                    //  true/false, Default: false - changes the origin of the host header to the target URL
                    changeOrigin: true,
                    //  true/false, Default: false - specify whether you want to ignore the proxy path of the incoming request 
                    ignorePath: true
                });
            });//dash
            self.app.get('/_dash-dependencies', function (req, res) {
                var target_uri = self._options.proxy.dash.uri + req.url;
                proxy.web(req, res, {
                    //true/false, passes the absolute URL as the path (useful for proxying to proxies)
                    toProxy: false,
                    //true/false, Default: true - specify whether you want to prepend the target's path to the proxy path
                    prependPath: true,
                    //  url string to be parsed with the url module
                    target: target_uri,
                    //  true/false, Default: false - changes the origin of the host header to the target URL
                    changeOrigin: true,
                    //  true/false, Default: false - specify whether you want to ignore the proxy path of the incoming request 
                    ignorePath: true
                });
            });//dash
            self.app.post('/_dash-update-component', function (req, res) {
                var target_uri = self._options.proxy.dash.uri + req.url;
                proxy.once('proxyReq', function (proxyReq, req, res, options) { //restream
                    let bodyData;
                    if (!req.body || !Object.keys(req.body).length) {
                        return;
                    }
                    switch (proxyReq.getHeader('Content-Type')) {
                        case "application/json":
                        case "application/json; charset=UTF-8":
                            if (typeof req.body == "object") { // request body is a object
                                bodyData = JSON.stringify(req.body);
                                console.log("________________________JSON BODY\n",bodyData)
                            } else { // request body is a JSON string
                                bodyData = req.body;
                            }
                            break;
                        case "application/x-www-form-urlencoded":
                            bodyData = querystring.stringify(req.body);
                            break;
                        default:
                    }
                    if (bodyData) {
                        proxyReq.setHeader('Content-type', "application/json");
                        //proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
                        console.log("________________________BODY\n",bodyData)
                        proxyReq.write(bodyData);
                        proxyReq.end();
                    }
                });
                proxy.web(req, res, {
                    xfwd: true,
                    followRedirects: true,
                    cookiePathRewrite: true,
                    cookieDomainRewrite: true,
                    //  url string to be parsed with the url module
                    target: target_uri,
                    //  true/false, Default: false - changes the origin of the host header to the target URL
                    changeOrigin: true,
                    //  true/false, Default: false - specify whether you want to ignore the proxy path of the incoming request 
                    ignorePath: false
                });
            });//dash
            self.app.get('/_reload-hash', function (req, res) {
                var target_uri = self._options.proxy.dash.uri + req.url;
                proxy.web(req, res, {
                    //  url string to be parsed with the url module
                    target: target_uri,
                    //  true/false, Default: false - changes the origin of the host header to the target URL
                    changeOrigin: true,
                    //  true/false, Default: false - specify whether you want to ignore the proxy path of the incoming request 
                    ignorePath: false
                });
            });//dash

在所有代理uri中,只有POST出现此问题,导致React在页面中失败:

api.js:73 TypeError: Cannot read property 'reloadHash' of undefined
    at Reloader.componentDidUpdate (Reloader.react.js:35)
    at e.notifyAll (react-dom@15.4.2.min.js?v=0.19.0&m=1551468960:12)
    at r.close (react-dom@15.4.2.min.js?v=0.19.0&m=1551468960:14)
    at r.closeAll (react-dom@15.4.2.min.js?v=0.19.0&m=1551468960:15)
    at r.perform (react-dom@15.4.2.min.js?v=0.19.0&m=1551468960:15)
    at o.perform (react-dom@15.4.2.min.js?v=0.19.0&m=1551468960:15)
    at o.perform (react-dom@15.4.2.min.js?v=0.19.0&m=1551468960:14)
    at Object.T [as flushBatchedUpdates] (react-dom@15.4.2.min.js?v=0.19.0&m=1551468960:14)
    at r.closeAll (react-dom@15.4.2.min.js?v=0.19.0&m=1551468960:15)
    at r.perform (react-dom@15.4.2.min.js?v=0.19.0&m=1551468960:15)

有关请求/响应的更多日志记录

GENERAL
Request URL: http://localhost:3000/_dash-update-component
Request Method: POST
Status Code: 405 METHOD NOT ALLOWED
Remote Address: 127.0.0.1:3000
Referrer Policy: no-referrer-when-downgrade

RESPONSE HEADERS
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept
Access-Control-Allow-Origin: *
allow: GET, HEAD, OPTIONS
connection: keep-alive
content-length: 178
content-type: text/html
date: Mon, 04 Mar 2019 11:53:15 GMT
server: Werkzeug/0.14.1 Python/3.6.7
Vary: Accept-Encoding
X-Powered-By: Express

[更新]

我已经实现的一种可能的方法是here。 express应用程序实现了一个HTTP代理,该代理可拦截Dash回调并呈现回Express。

0 个答案:

没有答案