Using Node.js
The entire purpose of Node.js is to execute JavaScript code. Open a command prompt, navigate to a convenient location, and create a folder named tools
. Add a file named hello.js
to the tools
folder, with the content shown in Listing 2.4:
Listing 2.4: The Contents of the hello.js File in the tools Folder
console.log("Hello, World");
The Node.js API has some features that are also provided by modern JavaScript browsers, including the console.log
method, which writes a message to the console. Run the command shown in Listing 2.5 in the tools
folder to execute the JavaScript code:
Listing 2.5: Executing the JavaScript Code
node hello.js
The node
command starts the Node.js runtime and executes the specified JavaScript file, producing the following output:
Hello, World
That’s all there is to know about executing JavaScript code. The rest of the functionality that Node.js provides is delivered through an API, which is described in the rest of this book, starting with Chapter 4.