basePath 注意事项edit

所有从 Kibana UI 到服务端的通信,都要遵守 server.basePath 。下面是一些基于上下文处理该问题的建议:

<img><a> 元素edit

如您所愿,可以不写基本路径而直接写上 src 或者 href urls,然后用 kbn-srckbn-href 替换 srchref

<img kbn-src="plugins/kibana/images/logo.png">

获取一个静态的 asset urledit

用 webpack 导入 asset 到 build 中。这会给您一个 JavaScript 的 URL,并给 webpack 一个机会去执行优化及中断缓冲。

// in plugin/public/main.js
import uiChrome from 'ui/chrome';
import logoUrl from 'plugins/facechimp/assets/banner.png';

uiChrome.setBrand({
  logo: `url(${logoUrl}) center no-repeat`
});

前端 API 请求edit

使用 chrome.addBasePath() 将 basePath 添加到 url 的前面。

import chrome from 'ui/chrome';
$http.get(chrome.addBasePath('/api/plugin/things'));

服务器端edit

在每个绝对 URL 路径上添加 config.get('server.basePath')

const basePath = server.config().get('server.basePath');
server.route({
  path: '/redirect',
  handler(req, reply) {
    reply.redirect(`${basePath}/otherLocation`);
  }
});

开发模式中的 BasePathProxyedit

Kibana 开发服务器在一个有随机 server.basePath 的代理之后自动运行。这样,当开发者编写代码时,他们会不断地确认他们的代码是否与 basePath 一起工作。

为了达到这个效果, serve 任务做了如下一些事情:

  1. 根据 dev.basePathProxyTarget 配置服务器的端口(默认 5603
  2. server.port 启动一个 BasePathProxy

    • randomBasePath 挑选一个三位数的值
    • /{any}/app/{appName} 重定向到 /{randomBasePath}/app/{appName} ,从而使刷新起作用
    • 将以 /{randomBasePath}/ 开头的所有请求代理到 Kibana 服务器

有时候,在开发环境中代理会产生意想不到的副作用。因此,当需要的时候您可以通过传递 --no-base-path 选项到 serve 任务或者 npm start 以退出代理。

npm start -- --no-base-path