In this post we are going to do some simple DOM manipulation and take text input and add it to a ul as a li in the browser. We are going to use Bootstrap 4 for our UI and keep it really simple.
First we want to get a game plan together, so I map out the following for the HTML:
<!--Input and Submit Button -->
<!-- Output Journal Entries as list items -->
And the following for the JS:
//add event listener to listen for either enter keypress or submit button clicked
//id="entryInput"
//id="entryInputButton"
// on enter or submit button clicked we want li element to be added to the top
// of the list so most recent journal entries are at the top
//id="journalUL"
// on enter or submit button clicked we want li element to be added to the top
// of the list so most recent journal entries are at the top
//create an html element and add it to the DOM
The first thing I do is head over to getbootstrap.com and copy the JS and CSS CDN code.
Tracking our thoughts. It is something we all think about doing at one time or another. Myself I have tried to start a journal and keep up with it countless times. I usually get several days in to a couple weeks then move on to something else.
I don’t think that is such a bad thing. Sometimes you need to think something out over a couple days and then you are good to go.
So today we are going to build a Command Line Journal App. You can run it from your powershell terminal and it will check for a journal.txt file and if no such file exists it will create one and take entries until you tell it to stop or quit.
Our journal is going to contain a couple of different parts with multiple tasks in each part, so I want to take a sec to add some comments that will serve as a plan for the application.
// create a variable for file path
// prepare rl variable
// check if a file exists
// if file exists open the file
// // open the file
// Ask for users name and then write name in Journal by template string
// get input from user
// Create a file by writing the word journal to the file
// if input is exit or quit then rl.close
So we want to try to open an existing file and if we throw an error (the file doesn’t exist) then we want to alert the user of the error, let them know we are creating a file and then create a file with the text “Journal inside:
let textIn ;
try {
// open the file
textIn = fs.readFileSync(FILEPATH, 'utf-8');
} catch (err) {
console.log('Error', err);
console.log('Creating File...');
// Ask for users name and then write name in Journal by template string
//TODO at Later Date
// Create a file by writing the word journal to the file
fs.writeFileSync(FILEPATH, 'Journal');
}
Now that we have a file open and stored in a variable we have to accept some input from the user and write it to the file. I initially try to use a while loop to continue to take input from the user and write to file until the entry is quit, but this created a loop that crashed VS Code.
The other suggestions looked interesting but I wasn’t able to wrap my head around them at this time. I copied the recursion code to my file and commented out my old erroneous code. Then I copied what was necessary from my code replacing the example code to come up with the following code:
var recursiveAsyncReadLine = function (textIn) {
rl.question('Journal Entry: ', function (entry) {
if (entry == 'exit' || entry == 'quit') //we need some base case, for recursion
return rl.close(); //closing RL and returning from function.
fs.writeFileSync(FILEPATH, `${textIn} \n (${Date.now()}) - ${entry}`);
recursiveAsyncReadLine(); //Calling this function again to ask new question
});
};
recursiveAsyncReadLine(textIn);
Finally, we need to exit the app on “close” and output the file to the console for all to see.
That did it! We now have a command line app that will read a file or create one if journal.txt doesn’t exist and then ask for input from the user and will write each entry to a file until the user types “quit” or “exit” into the prompt.
Check out the video, like, subscribe and leave a comment. Thanks for your time!
Yesterday, I learned how to read from a file. Today, I wanted to learn the natural extension of yesterday’s lesson and learn to write to a file! Along the way, I decided I needed to get some input from the command line so I could write dynamic info to my Welcome.txt file.
Just like yesterday, we will be using the fs module bundled with Node. So we start out with
const fs = require('fs');
Next we want to write something new to the file. (WARNING: If you don’t read your file first and append the new data to the old you will overwrite your entire file)
Each time I wanted to add something new to the file I had to change my source code and that was obviously not ideal. So we need a way to input some new text for our file from the command line and luckily we found the readline module. You can import it with the following require statement:
Next we use the rl variable’s question method to set up a question and then pass the input from the command line into a callback function where we write that text input to our file. Inside this callback function we use the rl variable’s close method to stop the input stream. This looks like the following:
Finally we want to stop the process and return to the command prompt using the rl variables on method where we pass the phrase “close” and a callback function where we log the resulting text file in the console and then use process.exit to kill the program.
We read input from the command line and we wrote that input to a file. Tomorrow we may try to loop this until we decide to quit or something else. Not sure. Thanks for reading and following along. Message me on twitter @jasonmziegler
Why because I have a larger goal of being a Javascript master. I want to “spin up” apps and create fun things that will help me and help the world. I believe I can do it. This begins my journey.
Let’s go!
So first thing I create a file in my Visual Studio Code IDE.
I call mine deadset001_readfile.js
Here is where we will write the code to read the txt file.
Next I create a text file to read.
I call it Welcome.txt and write Welcome to Deadset on Success Coding! Thank you for watching.
First thing we want to do is to require the fs module. This is built in to node so you don’t have to install anything.
Do you not have Node.js? Well, go get it. No we won’t wait.
So type in to your code editor
const fs = require('fs');
Now we have access to a variable that contains all of the filesystem methods that we need to read a file from our disk.
console.log('The file contains the following text: ", welcome);
ok, right click on your file in the explorer of VS code and select open in integrated terminal. This will take you to the directory where your file exists. You got Node.js, right?
Great! type into the terminal node filename.js in my case I wrote node ./deadset001_readfile.js
Now hit enter!
You should see the following out put
The file contains the following text: Welcome to Deadset on Success Coding! Thank you for watching.