Project design
- Node.js course
- Beginner
- Lesson 112/116
- 6 min
Design the skeleton of the beginner final mini app.
Project: Task tracker
In this 5-lesson project you will build a small task tracker app. Each lesson adds one piece; the last lesson gives you a working command line app. The app saves and loads tasks from a file.
First we must define the data file path. In Node.js the path module builds file paths in a cross-platform way. The call path.join("data", "tasks.json") gives the string "data/tasks.json". It uses the right separator on both Linux and Windows. Each lesson's beginning code includes the previous lesson's solution; the app grows step by step.
Using path.join
path.join joins multiple path parts into one path. path.join("data", "tasks.json") gives "data/tasks.json". path.join(__dirname, "tasks.json") gives a full path to the current file's directory.
In this project we will define a constant DATA_FILE: const DATA_FILE = path.join("data", "tasks.json");. We will use this constant in later lessons with fs.writeFileSync and fs.readFileSync to save and load tasks. Keeping the path in one constant makes it easy to change the file name in one place.
Practice Define the DATA_FILE constant
Import the path module with require. Define DATA_FILE with path.join("data", "tasks.json"). Print "Ready" with console.log.
Starter code
// Import the path module and define the DATA_FILE constant
// write here
console.log("Ready");Sample solution
const path = require("path");
const DATA_FILE = path.join("data", "tasks.json");
console.log("Ready");Expected output
Hazır
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which module builds file paths?
path.join("data", "tasks.json")- const path = require("path") Correct answer
- const fs = require("fs")
- const path = require("http")
- const os = require("os")
Explanation: The path module builds OS-correct paths.
2 Complete the path.join call
const DATA_FILE = path.join("____", "tasks.json");
const DATA_FILE = path.join("data", "tasks.json");
Explanation: 'data' is the folder name.
3 Match each piece to its role
| Term | Meaning |
|---|---|
path |
Path module |
path.join |
Joins path parts |
DATA_FILE |
Data file path |
require |
Imports a module |