Windows 上的 Node.Js - 如何清除控制台
作为 Node.js 环境和哲学的新手,我想知道几个问题的答案。我已经下载了 Windows 安装程序的 Node.js 以及节点包管理器。Windows Cmd 提示符当前用于运行 NodeJS 应用程序。
cls 清除命令窗口或命令提示符中的错误。 node.js 是否有等效项? console.clear 不存在 ;( 或者以其他形式存在?
我通过下面的代码创建了一个服务器
var http = require("http"); http.createServer(函数(请求,响应){ 响应.writeHead(200, { “内容类型”:“文本/html” }); 响应.write(“你好世界”); console.log("欢迎世界")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");
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.
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?
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");
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");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(23)
这适用于 Linux。不确定窗户。
您可以使用如下方式“欺骗”用户:
This works on linux. Not sure about windows.
You can "trick" the user using something like this:
跨平台:适用于 Linux、Windows 7/10/11、Mac
不留换行符
清除回滚
\x1B
(或\033
使用已弃用的八进制文字表示同一字符)是 ASCII 转义字符此行为与
cls
命令相同在 Windows 上,clear
命令在 Linux/Mac 上Cross-platform: works on Linux, Windows 7/10/11, Mac
Doesn't leave a newline
Clears scrollback
\x1B
(or\033
which uses deprecated octal literal for the same character) is ASCII Escape characterThis behavior is identical to
cls
command on Windows andclear
command on Linux/Mac这主要针对Linux;但据报道也可以在 Windows 中工作。
Gnome 终端中有 Ctrl + L 可以清除终端。它可以与 Python、Node JS 或在终端中运行的任何其他 REPL 一起使用。它与
clear
命令具有相同的效果。This is for Linux mainly; but is also reported to work in Windows.
There is Ctrl + L in Gnome Terminal that clears the terminal. It can be used with Python, Node JS or any other REPL running in terminal. It has the same effect as
clear
command.这将清除 Windows 上的控制台并将光标置于 0,0:
或
This clears the console on Windows and puts the cursor at 0,0:
or
我正在使用 Windows CMD,这对我有用
i am using a windows CMD and this worked for me
在 Windows 上处于严格模式时清除控制台:
And to clear the console while in strict mode on Windows:
从 Node.JS v8.3.0 开始,您可以使用方法 clear:
Starting from Node.JS v8.3.0 you can use method clear:
只需在 Windows 上使用
CTRL + L
即可清除控制台。Just use
CTRL + L
on windows to clear the console.尚未在 Windows 上测试过,但可以在 UNIX 上运行。诀窍在于
child_process
模块。检查文档。您可以将此代码保存为文件,并在每次需要时将其加载到 REPL。Haven't tested this on Windows but works on unix. The trick is in the
child_process
module. Check the documentation. You can save this code as a file and load it to the REPL every time you need it.使用严格模式解决问题:
To solve problems with strict mode:
使用官方的方式即可:
Just use the official way:
如果您使用
VSCode
,则可以使用CTRL + K
。我知道这不是通用解决方案,但可能会对某些人有所帮助。If you're using
VSCode
you can useCTRL + K
. I know this is not a generic solution but may help some people.根据 sanatgersappa 的回答和我发现的其他一些信息,我得出以下结论:
为了让事情变得更容易,我将其作为一个名为 cli-clear。
Based on sanatgersappa's answer and some other info I found, here's what I've come up with:
To make things easier, I've released this as an npm package called cli-clear.
您可以使用
readline
模块:readline.cursorTo(process.stdout, 0, 0)
将光标移动到(0, 0)。readline.clearLine(process.stdout, 0)
清除当前行。readline.clearScreenDown(process.stdout) 清除光标下方的所有内容。
You can use the
readline
module:readline.cursorTo(process.stdout, 0, 0)
moves the cursor to (0, 0).readline.clearLine(process.stdout, 0)
clears the current line.readline.clearScreenDown(process.stdout)
clears everything below the cursor.在 package.json 上尝试以下操作:
On package.json try this:
我无法让上述任何一个工作。我正在使用 nodemon 进行开发,发现这是清除控制台的最简单方法:
它只需滚动控制台几行,这样您就可以获得后续 console.log 命令的清晰屏幕。
希望它能帮助某人。
I couldn't get any of the above to work. I'm using nodemon for development and found this the easiest way to clear the console:
It just scrolls the console several lines so you get a clear screen for subsequent console.log commands.
Hope it helps someone.
此代码在我的 Node.js 服务器控制台 Windows 7 上运行良好。
process.stdout.write("\u001b[0J\u001b[1J\u001b[2J\u001b[0;0H\u001b[0;0W") ;
This code works fine on my node.js server console Windows 7.
process.stdout.write("\u001b[0J\u001b[1J\u001b[2J\u001b[0;0H\u001b[0;0W");
在 Mac 上,我只需使用 Cmd + K 来清除控制台,非常方便,并且比在项目中添加代码来执行此操作更好。
On mac, I simply use Cmd + K to clear the console, very handy and better than adding codes inside your project to do it.
这段代码对我来说效果很好
This code works fine for me
只是为这个问题添加一个解决方案。
我发现这个 gist 并且该解决方案对我有用,在视窗。
只是这样:
Just to add one more solution to this problem.
I found this gist and the solution works for me, on Windows.
Just this:
迟来了,但如果你使用 powershell,ctrl+l 可以在 Windows 中使用:) Powershell + Chocolatey + Node + npm = 获胜。
Belated, but ctrl+l works in windows if you're using powershell :) Powershell + chocolatey + node + npm = winning.
Ctrl + L 这是最好、最简单、最有效的选项。
Ctrl + L This is the best, simplest and most effective option.
就我而言,我这样做是为了永远循环,并在控制台中在一行中显示一个数字:
当您运行此代码时,您将看到相同的消息“队列测试队列中的消息数:1”。以及数字的变化(1..2..3等)。
In my case I did it to loop for ever and show in the console a number ever in a single line:
When you run this code you will see the same message "Number of messages in the queue Test Queue: 1." and the number changing (1..2..3, etc).