Вызов Dlot.ly Dash через обратный прокси-сервер Express.js - PullRequest
0 голосов
/ 04 марта 2019

Я использую обратный прокси-сервер Node.js / Express.js для обслуживания моей установки dash.Ресурсы шаблонов и javascript загружены нормально, но обновление API HTTP POST call /_dash-update-component возвращает метод 405 Not Allowed ошибка сервера Werkzeug/0.14.1 Python/3.6.7:

<!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

Из всех прокси-серверов Uris только у 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

[ОБНОВЛЕНИЕ]

Возможный подход, который я реализовал, - здесь .Приложение Express поддерживает http-прокси, который перехватывает обратные вызовы Dash и возвращает в Express.

...