Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Creating Your First Node.js App | Section
Node.js Fundamentals

bookCreating Your First Node.js App

メニューを表示するにはスワイプしてください

Let's take our first step into the world of Node.js by building a simple application. In this app, we'll generate a random number between 1 and 100 and display it on the screen. This exercise will help you get acquainted with creating and running a Node.js application on your local machine.

Note

We highly recommend working locally on your machine for the best learning experience.

Follow these steps to create your first Node.js app:

1. Create a New Project Directory

Begin by creating a new directory for your project. It must be the empty folder somewhere on your machine. Then open this folder in your code editor. We will use Visual Studio Code to demonstrate examples. You can select any that is convenient for you.

2. Create the app.js File

Inside the created directory, create a new file named app.js. This is where we'll write the code for our Node.js application.

3. Write the Application Code

const randomNumber = Math.floor(Math.random() * 100) + 1;
console.log("Random Number: ", randomNumber);

This code uses JavaScript's built-in Math.random() function to generate a random number between 0 and 1. By multiplying it by 100 and adding 1, we ensure that the result falls between 1 and 100.

4. Run the Application

Open your terminal or command prompt and ensure you're still in the directory with the app.js file. Run the following command to execute the app.js file using Node.js:

node app.js

You should see the output displaying the randomly generated number:

Random Number: <some_random_number>

Real World Scenario

To further enhance your understanding, you can watch the following video, which demonstrates how to create and run the app in a real-world scenario:

question mark

What command must be run in the terminal to start the Node app?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  7

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  7
some-alt