Heroku 将RedisDB与Express网关一起使用。如何从.ENV读取数据库凭据?
我正在使用Redis DB使用Heroku上的Express网关。 我想知道如何将Redis凭据设置为唯一的URL参数,而不是使用 分离变量 现在,Express Gateway从文件Heroku 将RedisDB与Express网关一起使用。如何从.ENV读取数据库凭据?,heroku,redis,express-gateway,heroku-redis,Heroku,Redis,Express Gateway,Heroku Redis,我正在使用Redis DB使用Heroku上的Express网关。 我想知道如何将Redis凭据设置为唯一的URL参数,而不是使用 分离变量 现在,Express Gateway从文件system.config.yml读取凭据,其中的凭据结构是 # Core db: redis: host: [host] port: [port] namespace: [namespace] password: [password] 不幸的是,Heroku上的Redis会以
system.config.yml
读取凭据,其中的凭据结构是
# Core
db:
redis:
host: [host]
port: [port]
namespace: [namespace]
password: [password]
不幸的是,Heroku上的Redis会以URL的形式自动将凭据设置为.ENV var:
REDIS_URL = "redis://h:[psw]@[host]:[port]"
如何使Express Gateway从.ENV而不是从system.config.yml
读取凭据?或者,如何使system.config.yml
从.ENV读取整个URL
更糟糕的是,凭据不是永久性的,并且会定期更改而不发出警报。
那么,如何让Express Gateway连接到Heroku上的Redis
谢谢您Express Gateway的配置文件-检查一下,您就可以开始了 我是这样解决的:在启动网关之前拆分变量,并设置环境变量
/*==============
server.js
================*/
const gateway=require('express-gateway');
const redis=require('redis')
const session=require('express-session')
让redistore=require('connect-redis')(会话)
require('dotenv').config();
var redis_Url=process.env.redis_Url;//Redis Heroku插件自动支持
//示例:redis://h:p81efd4c7e0ad310d7da303b7bd348d3d0bd9fcebc7aae61c9ba7c94a95a1290d@ec2-54-247-152-80.eu-west-1.compute.amazonaws.com:12799
变量组=/^redis:\/\/(.+?)\:(.+?)\@(.+?)\:(.+)$/gi.exec(redis\u Url);
process.env.NM=组[1];
process.env.PASW=组[2];
process.env.HOST=组[3];
process.env.R_PORT=组[4];/!!!不要将端口用作环境变量名称!!!
//运行网关
网关()
.load(path.join(\uu dirname,'config'))
.run();
及
谢谢,无论如何,唯一可用的.env变量是整个URL。我不知道如何在主机、密码等中拆分URL……是否可以在
system.config.yml
文件中使用正则表达式规则?这样,我就可以从整个URI中提取单个变量
#==============
# system.config.yml
#================
# Core
db:
redis:
host: ${HOST}
port: ${R_PORT}
namespace: EG
password: ${PASW}