Windows 上的 Node.Js - 如何清除控制台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9006988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:04:16  来源:igfitidea点击:

Node.Js on windows - How to clear console

windowsnode.jsconsolewindows-vista

提问by Deeptechtons

Being totally new into node.js environment and philosophy i would like answers to few questions. I had downloaded the node.js for windows installer and also node package manager.Windows Cmd prompt is being currently used for running nodejs apps.

作为 node.js 环境和哲学的新手,我想回答几个问题。我已经下载了用于 Windows 安装程序和节点包管理器的 node.js。Windows Cmd 提示符当前用于运行 nodejs 应用程序。

  1. cls clears the command window or errors in command prompt. Is there a equivalent for node.js ? console.clear does not exist ;( or does it in some other form?

  2. I created a server through this code below

    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");
    
  1. cls 清除命令窗口或命令提示符中的错误。node.js 是否有等价物?console.clear 不存在 ;( 还是以其他形式存在?

  2. 我通过下面的代码创建了一个服务器

    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");
    

i changed the code to below and refreshed the browser to find that content type does not change, how do i get to see the changes?

我将代码更改为下面并刷新浏览器发现内容类型没有改变,我如何才能看到变化?

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");

回答by sanatgersappa

console.log('3[2J');

This works on linux. Not sure about windows.

这适用于Linux。不确定窗户。

You can "trick" the user using something like this:

您可以使用以下方法“欺骗”用户:

var lines = process.stdout.getWindowSize()[1];
for(var i = 0; i < lines; i++) {
    console.log('\r\n');
}

回答by lfree

process.stdout.write('3c');

This also works on windows. Win7 at least.

这也适用于 Windows。至少是Win7。

回答by laktak

This clears the console on Windows and puts the cursor at 0,0:

这将清除 Windows 上的控制台并将光标置于 0,0:

var util = require('util');
util.print("\u001b[2J\u001b[0;0H");

or

或者

process.stdout.write("\u001b[2J\u001b[0;0H");

回答by Nishant

This is for Linuxmainly but is also reported to work in Windows.

这主要适用于Linux,但据报道也适用于 Windows。

There is Ctrl + Lin Gnome Terminal that clears the terminal as such. It can be used with Python, Node JS or any Interpreter presumably that uses terminal. I tend to clear many times hence this is very handy. Instaed of doing clear in Gnome Terminal you can just do Ctrl + L, it has nothing to do with the REPL running.

Ctrl + L在Gnome终端在清零终端本身。它可以与 Python、Node JS 或任何可能使用终端的解释器一起使用。我倾向于多次清除,因此这非常方便。Instaed 在 Gnome Terminal 中执行 clear 你可以只做Ctrl + L,它与 REPL 运行无关。

回答by guiweb

And to clear the console while in strict mode on Windows:

并在 Windows 上的严格模式下清除控制台:

'use strict';
process.stdout.write('\x1Bc'); 

回答by yousef

i am using a windows CMD and this worked for me

我正在使用 Windows CMD,这对我有用

console.clear();

回答by Lanil Marasinghe

Just use CTRL + Lon windows to clear the console.

只需CTRL + L在 Windows 上使用即可清除控制台。

回答by Abel Terefe

Haven't tested this on Windows but works on unix. The trick is in the child_processmodule. Check the documentation. You can save this code as a file and load it to the REPL every time you need it.

没有在 Windows 上测试过,但在 unix 上工作。诀窍在于child_process模块。检查文档。您可以将此代码保存为文件,并在每次需要时将其加载到 REPL。

var util = require('util');
var exec = require('child_process').exec;

function clear(){
    exec('clear', function(error, stdout, stderr){
        util.puts(stdout);
    });    
}

回答by Henri Cavalcante

To solve problems with strict mode:

用严格模式解决问题:

'use strict';
process.stdout.write('\x1B[2J');

回答by Hexodus

If you're using VSCodeyou can use CTRL + K. I know this is not a generic solution but may help some people.

如果您正在使用VSCode,则可以使用CTRL + K. 我知道这不是一个通用的解决方案,但可能会帮助一些人。