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");
console.log('\033[2J');
This works on linux. Not sure about windows.
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');
}
This clears the console on Windows and puts the cursor at 0,0:
var util = require('util');
util.print("\u001b[2J\u001b[0;0H");
process.stdout.write("\u001b[2J\u001b[0;0H");
process.stdout.write('\033c');
This also works on windows. Win7 at least.
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.
var util = require('util');
var exec = require('child_process').exec;
function clear(){
exec('clear', function(error, stdout, stderr){
util.puts(stdout);
});
}
This is not for Windows but for Linux .
There is Ctrl + L
in gnome-terminal that clears the terminal as such . It can be used with python nodejs 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 .
Based on sanatgersappa's answer and some other info I found, here's what I've come up with:
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);
}
To make things easier, I've released this as an npm package called cli-clear.
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:
console.log("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
It just scrolls the console several lines so you get a clear screen for subsequent console.log commands.
Hope it helps someone.
Belated, but ctrl+l works in windows if you're using powershell :) Powershell + chocolatey + node + npm = winning.
To solve problems with strict mode:
'use strict';
process.stdout.write('\x1B[2J');
If you use windows type cls command into console