Introduction to Node.js: Installation and Basics
Romulo Gatto
Posted on January 19, 2024
Introduction to Node.js: Installation and Basics
Node.js is a powerful JavaScript runtime built on Chrome's V8 engine. It allows developers to build scalable, high-performance applications using JavaScript on both the client and server sides. In this guide, we will walk through the process of installing Node.js and cover some of its basic features.
Installation
To get started with Node.js, you first need to install it on your machine. Here's how:
Step 1: Download
Head over to the official Node.js website at nodejs.org and click on the "Downloads" tab.
Choose the appropriate installer for your operating system (Windows, macOS, or Linux). For most users, it is recommended to select the LTS version as it provides stability and long-term support.
Step 2: Run Installer
Once downloaded, run the installation file and follow the instructions provided by the installer. The installation process may vary slightly depending on your operating system.
Step 3: Verify Installation
After successfully installing Node.js, open a command prompt or terminal window and enter node -v
. This command will display the installed version of Node.js. Similarly, you can check if npm (Node Package Manager) is also installed by entering npm -v
.
If both commands return valid versions without any errors, congratulations! You have successfully installed Node.js.
Getting Started with Basic Features
Now that we have Node.js up and running let's explore some of its basic features:
Running JavaScript Files
Using Node.js in a command prompt or terminal window allows you to execute JavaScript files directly. To do this:
- Create a new file called "example.js".
- Open "example.js" in a text editor.
- Add some basic JavaScript code like
console.log("Hello from Node!");
. - Save your changes and close the file.
- In a command prompt or terminal window, navigate to the directory where "example.js" is located.
- Enter the command
node example.js
. - You should see the message "Hello from Node!" printed in the console.
Congratulations! You have successfully executed your first JavaScript file using Node.js.
Creating a Simple Server
One of the main benefits of using Node.js is its ability to easily create web servers. Let's create a simple server that responds with "Hello, World!" when accessed:
- Create a new file called "server.js".
- Open "server.js" in a text editor.
-
Add the following code:
const http = require('http'); const server = http.createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello, World!'); }); const port = 3000; server.listen(port, () => { console.log(`Server running on port ${port}`); });
Save your changes and close the file.
In a command prompt or terminal window, navigate to the directory where "server.js" is located.
Enter
node test-server.js
into your command prompt or terminal window.Visit
http://localhost:3000
in your browser.You should see "Hello, World!" displayed.
That's it! You've created and run a basic web server using Node.js.
Conclusion
In this guide, we covered how to install Node.js on your computer and explored some of its basic features such as running JavaScript files and creating simple web servers.
Now that you have a solid foundation with Node.js installation and basics, you're ready to dive deeper into this powerful runtime environment and build amazing applications!
Happy coding!
Posted on January 19, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 29, 2024