layout | title | permalink |
---|---|---|
page |
Build a Dad Jokes Generator CLI |
index.html |
An intro to Node.js via a set of self-guided mini-projects from the command-line.
Any engineer who wants to:
- gain a deeper understanding of the Node API
- write their own Node.js framework
- author their own useful Node.js modules
- be able to fix Node.js libraries when they break, and submit pull requests to your favorite Node.js projects to make them faster and more secure
- A beginner audience who is hearing about your topic for the first time
- You should be comfortable in your terminal (shell) and have admin (sudo) privileges on your workstation.
- Experience in NodeJS is not required but recommended.
- Download the latest version of node.
- Text-editor (VSCode or Sublime)
- Source Code from Github
A conceptual overview of Node.js. What Node is, what V8 is, and how they both work, as well as how they relate to each other.
We'll talk about a few standard conventions used with Node.js, as well as the differences between writing javascript for Node.js and writing it for the browser.
Now we're going to build a Node.js CLI App that randomizes a set of corny dad jokes from a text file jokes.txt
. Doing this exercise will familiarize yourself with several key sections of the Node.js docs:
"fs"
, the file system,lib
,string decoder
, andpath
Our filesystem structure will look like this:
Image: "nodejs-dad-joke-generator-cli-project-structure.png"
Resources
- Tutorial Guide: Use
guide.md
, the step-by-step instruction guide.
Our project will contain the following files:
Image: "directory-tree.png"
We'll go through each file one by one to understand how they relate to each other.
Now that we've gone through all the files that make up the project, it's time to test our CLI app.
To start the app run:
node index.js
To stop the app run:
CTRL + C
Resources
- Final Source Code: You can find the complete source code for this example on GitHub
Today, we’ve learned how to create a simple Node.js CLI that prints random jokes to the console. We learned how to use the file system fs
module to get all the jokes and return them to the user.
Specifically, the process of how fs.readFileSync()
:
- Reads the text file containing the jokes.
- Turns the string into an array using regex.
- Returns the contents of the
path
.
If you want to get started with Node.js or need a bit of inspiration, here are some resources to check out.