简体   繁体   English

如何在 nodejs 上的 .env 中设置变量

[英]How to set variables in .env on nodejs

I have a script like this:我有一个这样的脚本:

const item = [
  companyid : 23,
  summaryid : 2
];

const url = process.env.urlLink;

And I want to set the url in .env like this:.env这样在.env设置url

urlLink = "https://example.com/print/index/companyid=" + item.companyid + "&summaryid=" + item.summaryid

When I console.log(url) , I want to get this value:当我console.log(url) ,我想得到这个值:

https://example.com/print/index/companyid=23&summaryid=2

But I always get this value instead:但我总是得到这个值:

https://example.com/print/index/companyid=" + item.companyid + "&summaryid=" + item.summaryid

How can I get the variable item.companyid and item.summaryid ?如何获取变量item.companyiditem.summaryid

Not a very elegant solution but it works不是一个非常优雅的解决方案,但它有效

on .env put在 .env 上

         companyid : 23
         summaryid : 2
         LINK = "https://example.com/print/index/companyid="

on your log put在你的日志上放

         console.log(process.env.LINK + process.env.companyid + "&summaryid=" + process.env.summaryid)

Try This :尝试这个 :

In .env file :在 .env 文件中:

 companyid = 23
 summaryid = 2
 LINK = "https://example.com/print/index/companyid="

in js file:在js文件中:

 console.log(process.env.LINK + process.env.companyid + "&summaryid=" + process.env.summaryid)

output:输出:

 https://example.com/print/index/companyid=23&summaryid=2

Just to keep the array that you have in your script, your .env file could look like this:只是为了保留脚本中的数组,您的 .env 文件可能如下所示:

urlLink = "https://example.com/print/index/companyid="
PARAM = "&summaryid="

And then your script然后你的脚本

console.log(process.env.urlLink + item.companyid + process.env.PARAM + item.summaryid)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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