Ask coding questions

← Back to all posts
Can I serve `.env` files to Express?
RahulChoubey1 (136)

So, in this repl, I want it so that if I'm the user, typing /.env will serve the .env file. But for other users, it should return a 403. But even Express doesn't know about my .env file. How do I fix this?

Answered by EpicGamer007 (1605) [earned 5 cycles]
View Answer
Comments
hotnewtop
EpicGamer007 (1605)

first you need a sign in, i see that u use repl auth.

what you can do is something like

app.get("/.env", (req, res) => {
  if(req.get("X-Replit-User-Name") === "RahulChoubey1") {

    res.send(process.env)

  } else {
    res.status(403).send("You are not allowed to access this resource");
  }
});
RahulChoubey1 (136)

@EpicGamer007 I forgot about process.env lol

get accepted

EpicGamer007 (1605)

@RahulChoubey1 thx lol, i will delete the example repl i attached now

Coder100 (16800)

HEY WHY ARE YOU NOT RESPONDING YOU MEANIE

Coder100 (16800)

anyways ez

app.get("/.env", (req, res) => {
  if (req.header("X-Replit-User-Name") == "WHATEVER UR NAME IS") res.type("text/plain").send("<INSERT THE ENV STUFF>");
  else DO WHATEVER YOU WANT WITH NON USERS
});
19wintersp (1121)

@Coder100 header is not a method of Express's Request object, did you mean the get method?

Coder100 (16800)

well u know that if you serve .env the secret will no longer be public lol

RahulChoubey1 (136)

@Coder100 According to the code, it will be served only if I'm the one online. Else, it will return a 403 for obvious reasons.