Getting Started with Node.js
If you're looking to dive into the world of server-side programming with JavaScript, Node.js is an excellent starting point. Node.js is a runtime environment that allows you to write server-side code using JavaScript. It's built on Chrome's V8 JavaScript engine, and it uses an event-driven, non-blocking I/O model, making it efficient and suitable for real-time applications. In this tutorial, we'll explore the basics of Node.js, from installation to writing your first server script.
1. Installing Node.js
To begin with Node.js, you first need to install it on your machine. Node.js can be downloaded and installed from its official website nodejs.org. It is available for various platforms including Windows, macOS, and Linux. Download the installer appropriate for your operating system, and follow the installation instructions. To verify that Node.js was installed correctly, open your terminal or command prompt and type the following command:
Copyable Code Example
node -v
This command will display the version of Node.js that is currently installed on your machine, indicating that Node.js is ready to use.
2. Your First Node.js Script
Once Node.js is installed, you can start writing JavaScript code that runs on a server. Let's create a simple script to understand how Node.js executes JavaScript. Create a new file named hello.js and open it in your favorite text editor. Copy and paste the following code into the file:
console.log('Hello, Node.js!');
Save the file and run it using Node.js from your terminal by navigating to the folder containing your file and running:
node hello.js
The output in your terminal should be:
Hello, Node.js!This simple example demonstrates how Node.js executes a JavaScript file. The
console.log
function prints a message to the terminal, similar to how it would in a browser's console.3. Creating a Web Server
One of the most common uses of Node.js is to create web servers. Let's create a basic web server that responds with "Hello, World!" to all requests. Create a new file named server.js and add the following code:
const http = require('http'); const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello, World!\n'); }); const port = 3000; server.listen(port, () => { console.log(`Server running at http://localhost:${port}/`); });
Save the file and run it using the command:
node server.js
Now, open a web browser and visit http://localhost:3000. You should see "Hello, World!" displayed in the browser. This server listens on port 3000 and responds to all requests with a plain text response.
4. Conclusion
This tutorial covered the basics of getting started with Node.js, including installation, writing simple scripts, and creating a basic web server. Node.js is a powerful tool for developing scalable network applications. As you become more familiar with its features and modules, you'll be able to build more complex applications such as real-time data processing systems, API services, and more. Keep experimenting and exploring the vast ecosystem of npm packages available to enhance your Node.js applications.
Happy coding!