Work & Development · Backend & Full Stack Development
Free preview
Run a JavaScript process outside the browser
Lesson 1 of Server Applications with Node.js. Anyone can read this lesson with no account. Every lesson after this one is for students.
Run JavaScript on the server
Run Node from a terminal, read the words passed to it and report whether the command succeeded.
After this lesson you will be able to run a Node program from the terminal, read its command-line arguments and set an exit status that tells other programs whether it succeeded.
1A program with a clear boundary
First finish Programming Foundations with JavaScript and Git and GitHub for Your Projects. A browser runs JavaScript inside a web page. It gives your code page tools such as document and window.
Node runs JavaScript outside the browser. A running program is called a process. The operating system, such as Windows or macOS, manages it. Node gives your program tools for files, networks and the process itself. It does not give it a browser page.
A server is a program that waits for requests. Think of a library desk: it stays open for the next question. A short calculation script finishes after doing its one job.
- Install Node.js 22 or newer from nodejs.org. The current LTS release, version 24, is a good choice. LTS means the release receives support for a longer time.
- Run
node --versionto check the installation. - Create a separate folder named
directory-api. Keep it outside your school application repository. - Write the exact Node version in a README, the file that explains how to use your project.
You need no cloud account for these lessons.
2Worked example: arguments and exit status
Save this as describe.mjs. The .mjs extension tells Node that this file uses JavaScript modules. Run node describe.mjs algebra from that folder.
const topic = process.argv[2];
if (!topic) {
console.error('Usage: node describe.mjs TOPIC');
process.exitCode = 1;
} else {
const resource = { title: 'Practice library', topic };
console.log(JSON.stringify(resource, null, 2));
}
You should see JSON with the topic algebra. JSON is a text format for sharing values such as objects and lists. Run the command again without algebra. This time it prints the usage message and reports failure.
process.argv is the list of words passed to the command. Positions 0 and 1 hold the Node program path and the script path. Your first extra word is at position 2. An exit status tells another program whether this command succeeded. Setting exitCode lets Node finish its output and cleanup before stopping.
A missing topic is an input mistake, so the script tells the user what to supply. That is different from a crash caused by broken code. Later, your server will give similar guidance in a response. When writing instructions, use example paths rather than assuming everyone has the same folders.
3Practice and debugging
- Create a command that takes a resource title and topic. Reject missing or whitespace-only values. Print the same JSON shape for every valid run.
- Try a title with spaces, first inside quotes and then without quotes. Count the arguments in each run and explain the difference.
- In a practice copy, refer to
document.body. Find theReferenceError, identify the browser-only object and fix the code. You do not need a browser-emulation library. - Run the script from its own folder, then from another folder using a relative path. Record which parts depend on the terminal's working folder.
4Check yourself
- Where does Node obtain process.argv?
- Does successful JSON output prove an HTTP server exists?
- Why preserve a nonzero exit status for invalid input?
Show answers
- The operating system passes in the list of words used to run the command.
- No: this script starts, prints, and exits without listening for requests.
- Shell scripts and automation can detect failure without interpreting prose.
5Project milestone
Commit the command and README to a new practice repository. Include three reproducible runs: valid input, missing input, and a title containing spaces. Your evidence is the output and your explanation, not a screenshot of installed tools.
Source: Node process documentation.
Continue with the full course
This free preview is lesson 1 only. Enroll to open the rest of Server Applications with Node.js.