# Setting Up Your First Node.js Application 

Node.js is a **cross-platform, open-source JavaScript runtime environment** that allows developers to execute JavaScript code outside a web browser to build server-side applications.

## **Installing Node.js :**

First, we need to install Node.js on our computer before writing the Node.js code.

When you install NodeJS, it also installs:

1.  The Node runtime
    
2.  NPM, the Node Package Manager, which helps us install libraries and packages.
    

**Step 1: Visit the Official Website**

**Open:** [**https://nodejs.org**](https://nodejs.org)

You will see two versions:

*   LTS (Recommended)
    
*   Current
    

For beginners, always download the **LTS version** because it is more stable.

**Step 2: Download Node.js :**  
Choose your operating system:

*   Windows
    
*   macOS
    
*   Linux
    

Download and install it like normal software.

**Step 3: Complete Installation :**

After installation finishes:

*   Restart the terminal if needed
    
*   Node.js is now ready
    

## **Checking Installation Using Terminal :**

Now we need to verify whether Node.js is installed correctly.

Open any of them in your system :

*   Command Prompt
    
*   PowerShell
    
*   Terminal
    
*   VS Code Terminal
    

# **Check Node Version**

Type:

```shell
node -v
```

Example Output:

```shell
v22.5.1
```

This means Node.js is successfully installed.

**Check npm Version :**

```shell
npm -v
```

```shell
10.8.2
```

This confirms npm is installed.

## **Understanding Node REPL :**

**REPL stands for:**

```plaintext
Read → Evaluate → Print → Loop
```

Node REPL is an interactive environment where we can directly write JavaScript code and instantly see the output.

Think of it like a mini JavaScript playground inside the terminal.  
Starting REPL

**To start it, simply type** `node` **in your terminal. You can now type JavaScript directly:**

```javascript
> 10 + 5
15
> const greeting = "Hello Node!"
undefined
> greeting
'Hello Node!'
```

To exit the REPL, press `Ctrl + C` twice or type `.exit`.

## Creating your first JS file

To create your first JavaScript file:

*   Create a folder for your project.
    
*   Inside that folder, create a file named `script.js`.
    
*   Open the file in any text editor (preferably in VS Code )and add the following:
    

```javascript
console.log("Node.js script is running successfully!");

//Node.js script is running successfully!
```

### Running the Script

To run the file using the `node` Command, open your terminal in VS Code and run the command below

```shell
node script.js
```

You should see the message printed directly in your terminal.

## **Writing Hello World server :**

Now, let us create a simple server using the built-in `http` module. This is the first step toward backend development.

Create a file named `server.js` and paste this code:

```javascript
const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});
```

### Execution Flow

1.  Run the server: `node server.js`.
    
2.  The terminal will display: `Server running at` [`http://127.0.0.1:3000/`](http://127.0.0.1:3000/).
    
3.  Open your browser and visit that URL to see your **"Hello World"** message.
