Industry-relevant training in Business, Technology, and Design to help professionals and graduates upskill for real-world careers.
Fun, engaging games to boost memory, math fluency, typing speed, and English skillsβperfect for learners of all ages.
Enroll to start learning
Youβve not yet enrolled in this course. Please enroll for free to listen to audio lessons, classroom podcasts and take mock test.
Listen to a student-teacher conversation explaining the topic in a relatable way.
Signup and Enroll to the course for listening the Audio Lesson
Today we're going to learn how to write our first Node.js program! Can anyone tell me what Node.js is?
I think it allows us to run JavaScript on the server!
That's right! Node.js lets us take JavaScript, traditionally a client-side language, and use it on the server side. Now, let's get practical and dive into writing a simple program.
What is the first thing we need to do?
We need to create a file called `hello.js`. This file will hold our JavaScript code. Who can tell me what we will write in this file?
Weβll use `console.log` to print a message, right?
Exactly! We'll write `console.log("Hello from Node.js!");`. Now, let me explain what this line does.
The `console.log()` function outputs messages to the console. Itβs like saying something aloud, only to the computer. Letβs write it together.
Signup and Enroll to the course for listening the Audio Lesson
Now that we've written our code, how do we run our Node.js program?
I think we use the command `node hello.js` in the terminal?
That's absolutely correct! You use the `node` command followed by the filename to execute your JavaScript file. Who can tell me what we'll see in the console after running the program?
It should print 'Hello from Node.js!'
Exactly! This output shows that our program ran successfully. It's our first taste of server-side JavaScript!
So, whatβs the significance of `console.log` again?
Great question! `console.log` is crucial for debugging and understanding what your program does as it runs. It'll help us see information as we build more complex applications.
Signup and Enroll to the course for listening the Audio Lesson
Alright class, let's summarize what we've learned today about writing our first Node.js program.
We created a file called `hello.js` and wrote a `console.log` statement.
And then we ran the program using `node hello.js` in the terminal!
Yes! Remember, each time we execute a program, we're engaging with how Node.js operates. Keep practicing with simple programs as it lays the foundation for more advanced coding.
What's the next step for us after this?
Next, we will learn how to create a web server using Node.js. It will be an exciting step forward to see how we can serve content to users!
Read a summary of the section's main ideas. Choose from Basic, Medium, or Detailed.
This section guides you through the process of writing a basic Node.js program. You will create a file that outputs a greeting message using the console.log method and then run it to see the output.
In this section, we introduce the foundational step of writing and executing your first Node.js program. The objective is to engage with the Node.js runtime environment by creating a simple JavaScript file named hello.js
which utilizes the console.log()
function. This function outputs data to the terminal, allowing you to see results directly in the console when executed.
hello.js
is located. Then, enter the command:This will execute the script, and the console will display:
Hello from Node.js!
This simple program lays the groundwork for more complex applications you will build as you dive deeper into Node.js and backend development.
Dive deep into the subject with an immersive audiobook experience.
Signup and Enroll to the course for listening the Audio Book
Create a file called hello.js
and write:
console.log("Hello from Node.js!");
In this first step, you're creating a new JavaScript file named hello.js
. Inside this file, you add a single line of code that uses the console.log
function. This function prints text to the console, which is the terminal or command prompt window where you're running your Node.js application.
Creating this file and adding code is similar to writing a note. Just as you would write your message on a piece of paper to share with someone, you are writing JavaScript code in a file to share instructions with the computer.
Signup and Enroll to the course for listening the Audio Book
Then run it using:
node hello.js
After you've saved the hello.js
file, the next step is to run the program. You do this by typing the command node hello.js
in your terminal or command prompt. The command node
tells the system to use the Node.js runtime to execute your JavaScript code contained in hello.js
.
Think of this step as starting a movie after you've created a script. You need to run the script (or in this case, execute the file) to see the characters (or in this case, the output) come to life on screen.
Signup and Enroll to the course for listening the Audio Book
This prints:
Hello from Node.js!
When you run the hello.js
program, you will see the output Hello from Node.js!
printed in your terminal. This confirms that your program is functioning correctly and that Node.js can run your JavaScript code on the server side.
This output is like a reply that you receive after sending a message. Just as you want to know if your message was delivered and perhaps even acknowledged, seeing the output means your instructions were successfully executed.
Signup and Enroll to the course for listening the Audio Book
β Congratulations! You've just run your first server-side JavaScript program.
Finally, when you see the output, it's a moment to acknowledge your achievement. You've successfully written and executed your first Node.js program, which means you can now start experimenting more with JavaScript in a new, server-side environment.
This is like being able to ride a bike for the first time. After practicing, you finally did it! Similarly, writing and running your first program opens the door to many more projects and creative possibilities in coding.
Learn essential terms and foundational ideas that form the basis of the topic.
Key Concepts
Node.js: A JavaScript runtime built on Chrome's V8 engine, enabling server-side execution.
File Creation: Writing JavaScript code in a .js
file which serves as the entry point for Node.js.
console.log: A method used to output information to the console, important for debugging.
See how the concepts apply in real-world scenarios to understand their practical implications.
Example of writing code in hello.js
: console.log('Hello from Node.js!');
.
After running the program with node hello.js
, the expected output will be Hello from Node.js!
.
Use mnemonics, acronyms, or visual cues to help remember key information more easily.
To run your code and let it show, type node, filename, and watch it flow.
Picture a talkative console that eagerly waits for you to say something through your JavaScript code. When it hears console.log
, it excitedly calls out what you've coded!
Remember the 'F-C-R' for Node.js execution: File creation, Code writing, Run command.
Review key concepts with flashcards.
Review the Definitions for terms.
Term: Node.js
Definition:
An open-source runtime environment that allows the execution of JavaScript on the server side.
Term: console.log
Definition:
A function in JavaScript that outputs messages to the console, useful for debugging and logging.
Term: JavaScript File
Definition:
A file containing JavaScript code, typically with a .js
extension.
Term: Terminal
Definition:
A command-line interface used to run commands and execute programs.