简体   繁体   中英

How do I hide my API keys while simultaneously letting my app having access to them in Heroku?

I have created a Node application and I'm finally ready to deploy it to Heroku. In the application I'm using several API keys. These I have put in a separate file and using exports to retrieve them from around my application. I then added the APIkey file to .gitignore. I've since then deployed my application to Heroku but it didn't work.

Here's my log while trying to be opening my app:

2018-12-19T23:38:14.937951+00:00 heroku[web.1]: Starting process with command npm start

2018-12-19T23:38:16.868566+00:00 app[web.1]:

2018-12-19T23:38:16.868584+00:00 app[web.1]: > weather-app@1.0.0 start /app

2018-12-19T23:38:16.868587+00:00 app[web.1]: > node app.js

2018-12-19T23:38:16.868588+00:00 app[web.1]:

2018-12-19T23:38:17.072318+00:00 app[web.1]: Starting geocode

2018-12-19T23:38:17.291050+00:00 heroku[web.1]: State changed from starting to crashed

2018-12-19T23:38:17.274770+00:00 heroku[web.1]: Process exited with status 1

2018-12-19T23:38:25.032443+00:00 heroku[web.1]: Starting process with command npm start

2018-12-19T23:38:28.893393+00:00 app[web.1]:

2018-12-19T23:38:28.893409+00:00 app[web.1]: > weather-app@1.0.0 start /app

2018-12-19T23:38:28.893411+00:00 app[web.1]: > node app.js

2018-12-19T23:38:28.893413+00:00 app[web.1]:

2018-12-19T23:38:29.228923+00:00 app[web.1]: Starting geocode

2018-12-19T23:38:29.529491+00:00 app[web.1]: internal/modules/cjs/loader.js:583

2018-12-19T23:38:29.529503+00:00 app[web.1]: throw err;

2018-12-19T23:38:29.529504+00:00 app[web.1]: ^

2018-12-19T23:38:29.529506+00:00 app[web.1]:

2018-12-19T23:38:29.529508+00:00 app[web.1]: Error: Cannot find module '../APIkeys'

2018-12-19T23:38:29.529510+00:00 app[web.1]: at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)

2018-12-19T23:38:29.529512+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:507:25)

2018-12-19T23:38:29.529514+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:637:17)

2018-12-19T23:38:29.529517+00:00 app[web.1]: at require (internal/modules/cjs/helpers.js:22:18)

2018-12-19T23:38:29.529519+00:00 app[web.1]: at Object. (/app/geocode/geocode.js:4:16)

2018-12-19T23:38:29.529521+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:689:30)

2018-12-19T23:38:29.529522+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)

2018-12-19T23:38:29.529524+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:599:32)

2018-12-19T23:38:29.529526+00:00 app[web.1]: at tryModuleLoad (internal/modules/cjs/loader.js:538:12)

2018-12-19T23:38:29.529528+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:530:3)

2018-12-19T23:38:29.546841+00:00 app[web.1]: npm ERR! code ELIFECYCLE

2018-12-19T23:38:29.547798+00:00 app[web.1]: npm ERR! errno 1

2018-12-19T23:38:29.549579+00:00 app[web.1]: npm ERR! weather-app@1.0.0 start: node app.js

2018-12-19T23:38:29.549782+00:00 app[web.1]: npm ERR! Exit status 1

2018-12-19T23:38:29.550057+00:00 app[web.1]: npm ERR!

2018-12-19T23:38:29.550295+00:00 app[web.1]: npm ERR! Failed at the weather-app@1.0.0 start script.

2018-12-19T23:38:29.550513+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

2018-12-19T23:38:29.562868+00:00 app[web.1]:

2018-12-19T23:38:29.563140+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:

2018-12-19T23:38:29.563302+00:00 app[web.1]: npm ERR!
/app/.npm/_logs/2018-12-19T23_38_29_554Z-debug.log

2018-12-19T23:38:29.673736+00:00 heroku[web.1]: State changed from starting to crashed

2018-12-19T23:38:29.648927+00:00 heroku[web.1]: Process exited with status 1

2018-12-19T23:38:51.067585+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=desolate-eyrie- 91272.herokuapp.com request_id=a200b9fa-8f0b-4d33-a1d2-eb990b0bd035 fwd="158.248.240.182" dyno= connect= service=

Now this is my first real application so my experience with this is limited and most likely my approach is incorrect. My question is: if my APIkeys shouldn't get added and therefore hidden in my .gitignore then what approach should I use to hide my keys but still making Heroku capable of running my app? My application works locally by doing npm start with the app.js.

Let me know if further information is needed.

I'd like to let you know that I've resolved my question. I had misunderstood what .gitignore actually did. Instead I've used a simple enviroment file and it works as a charm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM