router
以下需要使用最新版的 nohost:https://github.com/nohosts/nohost
nohost 集群部署时,用来分发请求到各个节点的路由器。
安装
npm i --save @nohost/router
使用
const Router = require('@nohost/router');
const {
SPACE_NAME,
GROUP_NAME,
ENV_NAME,
NOHOST_RULE,
NOHOST_VALUE,
CLIENT_ID,
CLIENT_ID_FILTER,
} = Router;
const router = new Router([
{
host: '10.11.12.13',
port: 8080
},
...
]);
router.proxy(req, res);
router.proxyUI(req, res);
更新服务器列表
router.update([
{
host: '10.11.12.13',
port: 8080
},
{
host: '10.31.32.33',
port: 8080
},
...
]);
router 每 12s 会检测一遍所有服务,并剔除不可用的
转发正常请求
const { headers } = req;
headers[NOHOST_RULE] = encodeURIComponent('ke.qq.com file://{test.html}');
headers[NOHOST_VALUE] = encodeURIComponent(JSON.stringify({ 'test.html': 'hell world.' }));
headers[SPACE_NAME] = encodeURIComponent('imweb');
headers[GROUP_NAME] = encodeURIComponent('avenwu');
headers[ENV_NAME] = encodeURIComponent('测试');
router.proxy(req, res);
查看抓包数据
headers[SPACE_NAME] = encodeURIComponent('imweb');
headers[GROUP_NAME] = encodeURIComponent('avenwu');
headers[ENV_NAME] = encodeURIComponent('测试');
router.proxyUI(req, res);
只转发到指定 Nohost 服务
const Router = require('@nohost/router');
const router = new Router({
host: '10.x.x.x',
port: 8080,
});
router.proxy(req, res);
router.proxyUI(req, res);
具体实现参考:测试用例