WARNING: This is not the most secure way to access config vars from heroku. If you have very sensitive keys that you don't want to be vulnerable then find a more secure way to do this.
I used the Heroku-Client API to pull in Config Vars from a specific heroku app using node.js and use a get request to send them to the front-end.
//server.js
const express = require('express');
const http = require('http');
const path = require('path');
const Heroku = require('heroku-client')
const heroku = new Heroku({ token: process.env.API_TOKEN })
const app = express();
let API_KEY = '';
app.use(express.static(path.join(__dirname, 'dist')));
heroku.request({
method: 'GET',
path: 'https://api.heroku.com/apps/name-of-app/config-vars',
headers: {
"Accept": "application/vnd.heroku+json; version=3",
"Authorization": "Bearer "+process.env.API_TOKEN
},
parseJSON: true
}).then(response => {
//console.log(response, "heroku api...");
API_KEY = response.API_KEY;
})
app.get('/heroku-env', function(req, res){
res.json(TOKEN);
});
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/index.html'))
});
const port = process.env.PORT || '3001';
app.set('port', port);
const server = http.createServer(app);
server.listen(port, () => console.log(`Running on localhost:${port}`));
Then use a service with HTTP to get the response to the frontend.
//EnvService.js
import { Injectable } from '@angular/core';
import { HttpClient, HttpResponse, HttpHeaders, HttpRequest } from '@angular/common/http'
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
@Injectable()
export class EnvService{
env:Observable<any> = null;
constructor(private http:HttpClient){}
getEnv() {
console.log("trying to get heroku env...");
this.env = this.http.get('/heroku-env')
return this.env;
}
}
Import the service into app.module then subscribe for the res in your component to access the config vars.
//my.component.js
import { Component, ViewChild, Inject } from '@angular/core';
import { EnvService } from './env.service';
@Component({
selector: 'my-component',
templateUrl: './my.component.html',
styleUrls: ['./my.component.css']
})
export class MyComponent {
defineToken: string = '';
constructor(private envService: EnvService) {}
loadEnv() {
this.envService
.getEnv().subscribe(res => {
this.token = res;
})
}
ngOnInit() {
this.loadEnv();
}
}
references: https://github.com/heroku/node-heroku-client