简体   繁体   English

如何在不使用 npm 的情况下选择 windows 端子中的 NODE_ENV?

[英]How can I choose the NODE_ENV in windows terminal without using npm?

I'm studying a NodeJs course, the main purpose of this course is to learn NodeJS without using npm, so we reached a stage where we defined our environment variable for staging and production, and I couldn't switch between the NODE_ENV from cmd terminal, we have a config.js file that exports the environment, the index.js imports them and based on the NODE_ENV asked for, it give it to you.我正在学习NodeJs课程,本课程的主要目的是在不使用npm的情况下学习NodeJS,所以我们到了一个阶段,我们为staging和production定义了环境变量,我无法在cmd终端的NODE_ENV之间切换,我们有一个导出环境的 config.js 文件,index.js 导入它们并根据请求的 NODE_ENV 将其提供给您。 -so this is the config.js script: -所以这是 config.js 脚本:

 /*
**** Creating and exporting config variables 
*/

//
var environments = {};

//
environments.staging ={
    'port' : 3000,
    'envName' : 'staging',
}

environments.production ={
    'port' : 5000,
    'envName' : 'production',
}

//
var currentEnvironment = typeof(process.env.NODE_ENV) == 'string'  ?  process.env.NODE_ENV.toLowerCase() :'';

//
var environmentToExport = typeof(environments[currentEnvironment]) == 'object' ? environments[currentEnvironment] : environments.staging;

// 
module.exports = environmentToExport;

and the Index.js script is this: Index.js 脚本是这样的:

var http = require('http');
var url = require('url');
var stringDecoder = require('string_decoder').StringDecoder;
var config = require('./config');
var server = http.createServer((req,res)=>{ to many lines i coudln't paste them });
server.listen(config.port,()=>{
    console.log("Server listening on port Nº : "+config.port+" in "+config.envName+" environment ");
});

So how can I choose the NODE_ENV from terminal?那么如何从终端选择 NODE_ENV 呢?

As the NODE_ENV is an Environment Variable, you can change this option on the terminal session with the command set (on CMD):由于NODE_ENV是一个环境变量,您可以使用命令set (在 CMD 上)在终端 session 上更改此选项:

set NODE_ENV=production
node index.js

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

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