What is a CLI app?
CLI stands for Command Line Interface. A CLI app is also known as a console application. It uses a text interface for input and ouput.
What is node.js?
Node.js is a javascript runtime environment for developing server-side and networking applications. It is open source and cross-platform.
What is REPL?
REPL is an online IDE. It allows users to write code, and build apps and websites using a browser. Good thing about it is that you can share your projects easily and it can be linked with your GitHub account. REPL stands for read–evaluate–print loop. It also provides a free plan.
How to make a node.js app using REPL?
- Make an account on REPL.
- Click on New repl on the top-left corner of your homepage.
- Select the programming language (Node.js in this case) and give it a name (or REPL will give it for you), then click on Create repl.
- You will see 3 sections. The left-most is the Navigator area, the middle area is the Editor (where you write your code) and the right-most is the Console (where your app will execute when you click on Run). You're now ready to code in Node.js.
Packages used for the project
Get user's input synchronously.
Provides persistent data storage.
Taking input from the user with 'readline-sync'
First, we have to include the package:
var rs = require("readline-sync")
REPL will automatically add this package once you run the app. If you still face an error, you can add it manually from the navigation panel on the left by searching in Packages
Take input:
var name = rs.question("What is your name? ")
Process input: (use backtick when using a variable inside the string)
var message = `Hello ${name}!`
else you could also do
var message = "Hello " + name + "!"
Print Output:
console.log(message)
All together, this is how you take input from the user, process it and print the output.
var rs = require("readline-sync")
var name = rs.question("What is your name? ")
var message = `Hello ${name}!`
console.log(message)
Coloring the console with 'chalk'
Chalk is a great package when it comes down to beautifying the console.
Include the package:
let chalk = require('chalk')
You can use it in the code as easily as:
var name = rs.question(chalk.green("What is your name? "))
Data persistence with 'jsonbase'
jsonbase.com is great when it comes down to persisting small data, especially when you're dealing with json data.
Include the package:
let jsonbase = require('jsonbase.com')
Create your Token:
// Replace this with your token (any random string)
let TOKEN = 'pJdQYebgaFSSBUXxQhtgYKVxnUzvUCkXFHXBXadg'
Create your Store:
let store = jsonbase(TOKEN)
To write data: (I have used the custom key 'user' to save user data, you can choose your own)
var name = rs.question(chalk.green("What is your name? "))
var age = rs.question(chalk.green("What is your age? "))
let user = {
"name": name,
"age": age
}
store.write('user',user).then( () => {
console.log("Data saved successfully!")
})
To read data: (use the same key that you used while writing data)
store.read('user').then( resp => {
console.log(resp.data)
})
Yes, it's that simple to use!
Top comments (1)
Can I use this to persist CLI auth info for auth endpoints?