How to use .env with javascript to get Twitter API on Glitch. I have tried Express.js and it did not work

Okay… First of all, I know this topic sounds like something has been talking about on the Internet and I should found it so easy but, apparently not. At least what has been posted on the internet + StackOverflow + youtube is not exactly my situation. And as a newbie to Node.js it is very fustrasting.

So please help me out or point me to the right tutorail… please…

I am making a project that use Twiiter API keys so I would like to hide those from the public.
I know that to hide those key the best way is to create a server side something with Node.js and with Express.js thing could be easier.

My program use codebird.js to search for tweets and there is this cd(key,secretkey),… < to pass the keys into. When I use .env with Node.js/Express.js it did not go to the client side, which of couse then the key will be exposed to the public. I have tried serveral ways including working with PHP but nonetheless if I work with codebord.js the way I do now people still can see my keys from console.

I have a feeling that probably have a better way with this. It sounds so simple but I know I have missed something and my limited knowledge can’t solve it on my own.

My project has been organized like this:

const express = require("express");
const app = express();
const listener = app.listen(process.env.PORT, () => {
  console.log("Your app is listening on port " + listener.address().port);

When I try to run locally on my computer index.js didnot work on browser which I guess is somehow with my firewall but can give me the console.log() on terminal. I can’t test on glitch because I am fail to do so nothing work here.

If you want to do API requests with a secret token, you can create a simple server on whatever framework you use (express for ex.). Search for ‘API using express nodejs’ on Google.

On Glitch, .env file is available via process.env. On your local machine you can use dotenv library

1 Like

Is this mean I also have to move the function that I call for Twitter into express.js file? And then put information into json file to pass it to my public file (index.html and sketch.js) ?

Let’s say I have index.js file to work with express.js so I can use that “process.env.MYVALUE” on the server side… Can I use codebird.js function to get Twitter data exactly like how I code it in sketch.js?

YES I know that. my point is that it did not work the way i want and suppose to.

  1. I don’t know how to call that data in .env to the framework I use to call Twitter and don’t shows the key on the console still.
  2. the framework I work with is coded inside sketch.js so i am not sure how to move that into index.js which is no the server side.
  1. As I said, on Glitch you just call process.env[variable] and on your local machine you can use dotenv package. Documentation on how to use that variable you can find on web.

  2. Just do not use the API library on client, do that on server and on client side you have to make requests to that server.

1 Like