[英]Get string depending on the environment using module.exports or exports in nodejs
I'm new to NodeJs, I just couldn't find the right question, article, blog post, and whatnot about what I need to do.我是 NodeJs 的新手,我只是找不到合适的问题、文章、博客文章,以及我需要做什么。
Basically I want a function or a variable that returns a string for my Stripe keys based on the environment (ie Production and Development).基本上我想要一个 function 或一个根据环境(即生产和开发)为我的 Stripe 键返回字符串的变量。
I have this in my config.js我的config.js中有这个
// config.js
const dotenv = require("dotenv")
dotenv.config()
const stripeSK = function getStripeSK() {
if (process.env.NODE_ENV === "production") {
return "a"
} else {
return "b"
}
}
module.exports = {
stripeSK: stripeSK,
accessLevels: {
user: "user",
admin: "admin"
},
JWT_SECRET: "somesecret",
port: process.env.PORT,
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},
"development": {
"username": process.env.DEV_DB_USERNAME,...
...
But what I do get from console.log()
is [Function: getStripeSK]但是我从
console.log()
得到的是[Function: getStripeSK]
What's the proper way to do this?这样做的正确方法是什么?
In your code stripeSK
is assigned a function.在您的代码
stripeSK
被分配了一个 function。 While exporting it you need to call it and not just re-assign.导出它时,您需要调用它,而不仅仅是重新分配。 For eg:
例如:
module.exports = {
stripeSK: stripeSK(),
accessLevels: {
...
Additionally, you can skip crating function entirely and can do something like this:此外,您可以完全跳过装箱 function 并可以执行以下操作:
stripeSK: process.env.NODE_ENV === 'production' ? 'a': 'b'
Because you are not calling the function { stripeSK: stripeSK, ... }
?因为您没有调用 function
{ stripeSK: stripeSK, ... }
?
It should be like this:它应该是这样的:
{
stripeSK: stripeSK(),
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.