Does accessing NODE_ENV make sense in front end code?

前端 未结 4 2053
攒了一身酷
攒了一身酷 2021-02-19 15:30

I have a react/node app that I\'m deploying. One of the components attempts to access NODE_ENV in order to determine which host URL to use (localhost or heroku).

I now r

4条回答
  •  被撕碎了的回忆
    2021-02-19 16:00

    I think you're misunderstanding the scope here. Node is a javascript runtime which runs as a server to handle backend stuff. NODE_ENV is a variable used in node to set up environment variables, thus, it is only accessible in the node environment, not in the client side.

    Anyway in the first place, there is no reason why you would want to expose your config variables to your client side (that is a vulnerability issue). If it is related to some configs that relies on your server side variables, you have to set it in your client side independently. A use case I can imagine is defining a socket host and port to listen from the client side to the server side. Hope that clears things up :)

    Imagine the life cycle:

    1. client requests
    2. node server processes and responds to request
    3. response is served through the browser

提交回复
热议问题