node.js - Gitbash 未正确设置环境变量

标签 node.js git-bash npm-debug

我正在尝试在我的 Node 服务器中使用npm install debug

var debugModule = require('debug');
var debugMainApp = debugModule('debugMainApp')

const express = require('express');

const app =  express();

const port ='3000';
const domain = 'localhost';

app.listen(port,domain,()=>{
     debugMainApp('Server started in port: ' + port + ' hostname: ' + domain);
});

debugMainApp('服务器在端口启动:' + port + ' 主机名:' + 域); 没有向控制台打印任何内容。

我尝试解决此问题
通过手动设置属性 debugMain.enabled = true debugMainApp('Server started in port: ' + port + ' hostname: ' + domain); 将以下内容打印到控制台:

mainApp Server started in port: 3000 hostname: localhost +0ms

据我了解,当设置与字符串 debugModule('this_String') 匹配的环境变量时,应自动设置此 debugMain.enabled 属性。

这是我设置环境变量并启动服务器的方法

$ DEBUG=debugMainApp & node server.js

但似乎后者没有正确设置环境变量。

问题

  1. 如果你认为我的环境变量理论是正确的。使用 GitBash 命令行设置环境变量的正确方法是什么?例如$ DEBUG=mainApp & node server.js
  2. 会不会是别的什么?

提前致谢。

更多相关信息:
我的操作系统:Windows 10
GitBash 版本:2.13.0.windows.1
NodeJS 版本:6.10.3

解决方案 enter image description here

最佳答案

& 将指示 bash 在后台启动进程。

正确的语法是:

DEBUG=mainApp node server.js

查看更多信息“Why is setting a variable before a command legal in bash?

A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator.

关于node.js - Gitbash 未正确设置环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48873660/

相关文章:

node.js - Google Sheets API——自动获取最大范围的表格

node.js - NodeJS Express - 渲染页面自动更新

node.js - 如何为以下条件编写mongo聚合函数

node.js - Passport JS + Rest API +SPA

windows - Windows 版 Git-scm、msysGit 和 Git 之间的差异

windows - 调用 CMD 并将输入从 Bash shell 脚本传递给 EXE(使用适用于 Windows 的 Git Bash)

javascript - 找不到强大的命令

angular - 忽略 .gitignore 中名为 npm-debug.log.XXXXXX 的文件