繁体   English   中英

NPM nodemon并在终端中调试无输出

[英]NPM nodemon and debug no output in terminal

我有这个简单的代码:

const express = require('express');
const chalk = require('chalk');
const debug = require('debug');
const morgan = require('morgan');
const path = require('path');

const app = express();
const port = process.env.PORT || 3000;

app.use(morgan('tiny'));
app.use(express.static(path.join(__dirname, '/public')));
app.use('/css', express.static(path.join(__dirname, '/node_modules/bootstrap/dist/css')));
app.use('/js', express.static(path.join(__dirname, '/node_modules/bootstrap/dist/js')));
app.use('/js', express.static(path.join(__dirname, '/node_modules/jquery/dist')));

app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, '/views/index.html'));
});

app.listen(port, () => {
    debug(`listening on port ${chalk.green(port)}`);
});

问题是,无论我做什么,启动应用程序listening on port 4000listening on port 4000的输出都不会出现,但是其余的一切都正常,一切都会正常进行。 它应该100%工作,也许您可​​以帮助我。 谢谢!

根据文档和您的应用程序,这里是应用程序,已修改和输出的精简示例。

文档: https : //www.npmjs.com/package/debug

您的应用程序(经过微调并稍作修改)- 请注意something 这是调试附加的内容

const express = require('express');
const chalk = require('chalk');
const debug = require('debug')('something');

const app = express();
const port = process.env.PORT || 3004;

debug('some debug statement');

app.listen(port, () => {
    debug(`listening on port ${chalk.green(port)}`);
});

来自终端线路的呼叫:

$ DEBUG=something node ~/path_to_your_project/project_name/app.js

输出:

克里斯-MBP-3:untitled1 lcsharp $ DEBUG =某个节点

~/WebstormProjects/untitled1/app.js
  something some debug statement +0ms
  something listening on port 3004 +9ms

如果要使用debug模块,则必须使用名称空间初始化记录器:

const debug = require('debug')('my-namespace')

默认情况下, debug输出完全不记录,您需要使用DEBUG环境变量来定义应记录的内容。 如何使用取决于您使用的外壳,如下所示:

DEBUG=* node index.js

DEBUG=*表示已记录所有内容,因为*是通配符。

如果您只想记录my-namespace ,则必须为:

DEBUG=my-namespace node index.js

您可以将登录分为以下几个部分:

const debugApp = require('debug')('my-namespace:app')
const debugModuleA = require('debug')('my-namespace:module-a')
const debugModuleB = require('debug')('my-namespace:module-b')

使用my-namespace:*您可以记录appmodule-amodule-b

可以对nodemon执行相同的操作:

DEBUG=my-namespace nodemon index.js

暂无
暂无

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

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