对 node.js 环境和哲学是全新的,我想回答几个问题。我已经为 Windows 安装程序和节点包管理器下载了 node.js。Windows Cmd 提示符当前用于运行 nodejs 应用程序。
cls 清除命令窗口或命令提示符中的错误。 node.js 是否有等效项? console.clear 不存在;(或者以其他形式存在?
我通过下面这段代码创建了一个服务器
var http = require("http");
http.createServer(function (request, response) {
response.writeHead(200, {
"Content-Type": "text/html"
});
response.write("Hello World");
console.log("welcome world")response.end();
}).listen(9000, "127.0.0.1");
我将代码更改为下面并刷新浏览器,发现内容类型没有变化,我如何才能看到变化?
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
console.log("welcome world")
response.end();
}).listen(9000,"127.0.0.1");
console.log('\033[2J');
这适用于 linux。不确定窗户。
你可以使用这样的东西“欺骗”用户:
var lines = process.stdout.getWindowSize()[1];
for(var i = 0; i < lines; i++) {
console.log('\r\n');
}
process.stdout.write('\033c');
这也适用于 Windows。 Win7 至少。
这将清除 Windows 上的控制台并将光标置于 0,0:
var util = require('util');
util.print("\u001b[2J\u001b[0;0H");
process.stdout.write("\u001b[2J\u001b[0;0H");
这主要针对Linux;但据报道也可以在 Windows 中工作。
Gnome Terminal中有Ctrl + L清空终端。它可以与 Python、Node JS 或任何其他在终端中运行的 REPL 一起使用。它与
clear
命令具有相同的效果。
我正在使用 Windows CMD,这对我有用
console.clear();
在 Windows 上以严格模式清除控制台:
'use strict';
process.stdout.write('\x1Bc');
从 Node.JS v8.3.0 开始,您可以使用方法 clear:
console.clear()
只需在 Windows 上使用
CTRL + L
即可清除控制台。
没有在 Windows 上测试过,但可以在 unix 上运行。诀窍在于
child_process
模块。检查文档。您可以将此代码保存为文件,并在每次需要时将其加载到 REPL。
var exec = require('child_process').exec;
function clear(){
exec('clear', function(error, stdout, stderr){
console.log(stdout);
});
}
用严格模式解决问题:
'use strict';
process.stdout.write('\x1B[2J');
就用官方的方式:
console.log('Blah blah blah'); // Prints "Blah blah blah"
console.clear(); // Clears, or in other words, resets the terminal.
console.log('You will only see this message. No more Blah blah blah...');
如果您使用
VSCode
,您可以使用CTRL + K
。我知道这不是一个通用的解决方案,但可能会对某些人有所帮助。
根据 sanatgersappa 的回答和我发现的其他一些信息,这是我想出的:
function clear() {
var stdout = "";
if (process.platform.indexOf("win") != 0) {
stdout += "\033[2J";
} else {
var lines = process.stdout.getWindowSize()[1];
for (var i=0; i<lines; i++) {
stdout += "\r\n";
}
}
// Reset cursur
stdout += "\033[0f";
process.stdout.write(stdout);
}
为了让事情更简单,我将其发布为一个名为 cli-clear 的 npm 包。
您可以使用
readline
模块:
readline.cursorTo(process.stdout, 0, 0)
将光标移动到 (0, 0)。
readline.clearLine(process.stdout, 0)
清除当前行。
readline.clearScreenDown(process.stdout)
清除光标下方的所有内容。
const READLINE = require('readline');
function clear() {
READLINE.cursorTo(process.stdout, 0, 0);
READLINE.clearLine(process.stdout, 0);
READLINE.clearScreenDown(process.stdout);
}
在 package.json 上试试这个:
"nodemonConfig": {
"events": {
"start": "clear"
}
}
我无法使上述任何一项工作。我正在使用 nodemon 进行开发,发现这是清除控制台的最简单方法:
console.log("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
它只是将控制台滚动几行,这样您就可以清楚地看到后续的 console.log 命令。
希望对某人有所帮助。
此代码在我的 node.js 服务器控制台 Windows 7 上运行良好。
process.stdout.write("\u001b[0J\u001b[1J\u001b[2J\u001b[0;0H\u001b[0;0W");
在 mac 上,我只是简单地使用 Cmd + K 来清除控制台,非常方便,并且比在您的项目中添加代码来完成它更好。
迟来的,但如果您使用的是 powershell,则 ctrl+l 可以在 Windows 中使用 :) Powershell + chocolatey + node + npm = winning.
Ctrl + L 这是最好、最简单、最有效的选择。
在我的例子中,我这样做是为了永远循环并在控制台中显示一个数字:
class Status {
private numberOfMessagesInTheQueue: number;
private queueName: string;
public constructor() {
this.queueName = "Test Queue";
this.numberOfMessagesInTheQueue = 0;
this.main();
}
private async main(): Promise<any> {
while(true) {
this.numberOfMessagesInTheQueue++;
await new Promise((resolve) => {
setTimeout(_ => resolve(this.showResults(this.numberOfMessagesInTheQueue)), 1500);
});
}
}
private showResults(numberOfMessagesInTheQuee: number): void {
console.clear();
console.log(`Number of messages in the queue ${this.queueName}: ${numberOfMessagesInTheQuee}.`)
}
}
export default new Status();
运行此代码时,您将看到相同的消息“队列测试队列中的消息数:1”。和数字变化(1..2..3 等)。