Node.js
Node.js is a powerful, server-side JavaScript runtime that allows you to build scalable and efficient network applications. Node.js is designed for server-side (back-end) development, although it can also be used to run desktop applications such as scripts, utility applications, or even video games. In this introductory tutorial, we’ll explore the basics of Node.js and guide you through creating your first Node server.
Prerequisites
Before we dive into Node.js, you’ll need to have the following installed on your computer:
-
Node.js: Download and install Node.js from the official website (https://nodejs.org/). Node.js comes bundled with npm, the Node Package Manager, which we’ll use to install libraries and packages.
-
Text Editor: You’ll need a text editor to write JavaScript code. Popular choices include Visual Studio Code, Sublime Text, or any code editor of your preference.
What is Node.js?
Node.js is built on the V8 JavaScript engine from Google Chrome. It allows you to run JavaScript code outside of a web browser, making it ideal for server-side development. Node.js is known for its non-blocking, event-driven architecture, which makes it highly efficient and suitable for handling concurrent connections and real-time applications.
Creating Your First Node.js Server
Let’s create a simple Node.js server that listens on a specified port and responds with “Hello, World!” when accessed.
Step 1: Setting up Your Project
Create a new directory for your project and open your preferred text editor in that directory.
Step 2: Creating the Server File
In your text editor, create a file named server.js
. This will be the main file for your Node.js server.
Step 3: Writing Your Node.js Code
In server.js
, write the following code to create a basic HTTP server:
// Import the HTTP module
const http = require('http');
// Define the hostname and port
const hostname = '127.0.0.1';
const port = 3000;
// Create a server and handle incoming requests
const server = http.createServer((req, res) => {
res.statusCode = 200; // HTTP status code: OK
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, World!\n');
});
// Start the server and listen on the specified port and hostname
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
This code imports the built-in http
module, creates an HTTP server, and sets it to listen on port 3000. When a request is received, it responds with “Hello, World!”
Step 4: Running Your Node.js Server
Open your terminal or command prompt, navigate to your project directory, and run the following command:
node server.js
You should see the message “Server running at http://127.0.0.1:3000/” in the console.
Step 5: Testing Your Server
Open your web browser and visit http://127.0.0.1:3000/
. You should see the “Hello, World!” message displayed in your browser.
Congratulations! You’ve created your first Node.js server. This is just the beginning of your journey with Node.js, and you can explore its capabilities further to build dynamic web applications, APIs, and much more. Node.js offers a vast ecosystem of libraries and frameworks that empower you to tackle a wide range of projects, making it an exciting technology to master.