“Node.js is a platform built on Chrome’s Javascriptruntime for easily building fast and scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.”
Apart from the formal definition, some crucial points about node.js are:
It is an open-source server environment
Node.js applications are written in JavaScript and run on various platforms like Windows, Linux, Unix, Mac OS X.
It uses JavaScript on the server.
It is neither a framework nor a programming language
We use it to develop server-side and networking applications.
Now that we know about node.js, let's learn how node.js input and output from the command line works.
Accept Input from Command Line
To get input from a readable stream for example the process.stdin stream, which is the terminal input during the execution of a Node.js program, one line at a time.
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
})
readline.question(`What is your name?`, name => {
console.log(`Hello ${name}!`)
readline.close()
})
You can also try this code with Online Javascript Compiler
The code that you just went through asks the user for the username and once entered we send out a greeting.
The first parameter (the question) is shown out by the question() method and it waits for the user to enter the input.It then calls the callback function once the enter key is pressed.
The readline interface is closed in this callback function.
Another package that can help us to take input from command line is inquirer.
console.clear() is used to clear the console and the behavior depends on the console that we use.
Counting Elements
If we tend to count elements we can use a handy function called console.count() to get our job done.
Let’s have a look at this code :
const a = 1
const b = 2
const c = 3
console.count(
'The value of a is ' + a +
' and has it been checked how many times?'
)
console.count(
'The value of a is ' + a +
' and has it been checked how many times?'
)
console.count(
'The value of b is ' + b +
' and has it been checked how many times?'
)
You can also try this code with Online Javascript Compiler
The value of a is 1 and has it been checked how many times?: 1
The value of a is 1 and has it been checked how many times?: 2
The value of b is 2 and has it been checked how many times?: 1
You can also try this code with Online Javascript Compiler
This will now help us by printing the stack trace. We can also try this in the Node.js REPL and this is what we will get then:
Trace at function2 (repl:1:33) at function1 (repl:1:25) at repl:1:1 at ContextifyScript.Script.runInThisContext (vm.js:44:33) at REPLServer.defaultEval (repl.js:239:29) at bound (domain.js:301:14) at REPLServer.runBound [aseval] (domain.js:314:12) at REPLServer.onLine (repl.js:440:10) at emitOne (events.js:120:20) at REPLServer.emit (events.js:210:7)
Calculate the time spent
We can also calculate the time a function takes to run very easily using the time() and the timeEnd(). Let’s have a look at the code for the same:
So we used the console.log to print messages in the console and this is what we call the standard output or stdout.
Also,
console.error() prints directly to the stderr stream
And the main part about this is that it will never appear in the console but will appear in error log.
Color the output
We often wish to color the output of our text and we can easily do it using the escape sequences. It is basically a set of characters that help us by identifying a specific color. Let’s understand this with the help of an example:
console.log('\x1b[33m%s\x1b[0m', 'hello!')
You can also try this code with Online Javascript Compiler
This code will give us a direct output of “hello!” in yellow color.
But we can say that this is a very low-level way to do this. We can use a simpler way to do the same thing and that is by using a library. The name of this library is Chalk, and it not only colors but also helps us to style our output by making text bold, italic and also underline it.
We can install it using npm install chalk and use it in the below mentioned way:
import chalk from 'chalk';
console.log(chalk.green("Welcome To"))
console.log(chalk.bold("Coding Ninja"))
console.log(chalk.blue('Hello world!'));
You can also try this code with Online Javascript Compiler
Using the chalk.green and chalk.bold command we conveniently get our job done rather than worrying about the escape codes and this code is also pretty much easily readable
Create a progress bar
We have another fabulous package that helps us create a progress bar in the console. We can install it by using npm install progress.
This basically is a snippet of code that creates a 10-step progress bar, and after a time interval of 100ms one step gets completed. As soon as the bar gets completed we successfully clear the interval:
import ProgressBar from 'progress';
var bar = new ProgressBar(':bar', { total: 10 });
var timer = setInterval(function () {
bar.tick();
if (bar.complete) {
console.log('\ncomplete\n');
clearInterval(timer);
}
}, 100);
You can also try this code with Online Javascript Compiler
Finally after a deep study of the node.js input and output statements let's have a look at the frequently asked questions by people on the topic.
Frequently asked questions
1. Is Node JS frontend or backend?
Node.js can be used in both the frontend and backend of applications and websites, thereby making the work of developers easier.
2. Is Node JS a programming language?
No, we can say that node.js is not a programming language, it allows developers to use JavaScript, a programming language for creating web applications.
3. Name some companies that use Node.js.
Some companies that use node.js are eBay, PayPal, Yahoo, Microsoft, and Uber.
4. List some advantages of using Node.js.
Some advantages of Node.js are:
Easy Scalability
Real-time web apps
Fast Suite
Easy to learn and code
Caching
Data Streaming
Corporate Support
Key Takeaways
In this article we were able to understand the node.js input and output commands.We discussed node.js input and output in detail. We also discussed some frequently asked question about node.js input and output.
This doesn’t seem enough, though.
Of course not!
Whether it is frontend or backend or even both with node.js, Web development is all about learning by doing. You can now check out this course on Full Stack Development, which includes tutorials on Node.js.