basePath 注意事项
edit
IMPORTANT: This documentation is no longer updated. Refer to Elastic's version policy and the latest documentation.
basePath 注意事项
edit所有从 Kibana UI 到服务端的通信,都要遵守 server.basePath 。下面是一些基于上下文处理该问题的建议:
<img> 和 <a> 元素
edit如您所愿,可以不写基本路径而直接写上 src 或者 href urls,然后用 kbn-src 或 kbn-href 替换 src 或 href 。
<img kbn-src="plugins/kibana/images/logo.png">
获取一个静态的 asset url
edit用 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`);
}
});
开发模式中的 BasePathProxy
editKibana 开发服务器在一个有随机 server.basePath 的代理之后自动运行。这样,当开发者编写代码时,他们会不断地确认他们的代码是否与 basePath 一起工作。
为了达到这个效果, serve 任务做了如下一些事情:
-
根据
dev.basePathProxyTarget配置服务器的端口(默认5603) -
在
server.port启动一个BasePathProxy-
为
randomBasePath挑选一个三位数的值 -
从
/{any}/app/{appName}重定向到/{randomBasePath}/app/{appName},从而使刷新起作用 -
将以
/{randomBasePath}/开头的所有请求代理到 Kibana 服务器
-
为
有时候,在开发环境中代理会产生意想不到的副作用。因此,当需要的时候您可以通过传递 --no-base-path 选项到 serve 任务或者 npm start 以退出代理。
npm start -- --no-base-path