Starting from Scratch: Step-by-Step Guide to Setting Up Your First Node.js Server
Tags: javascript, beginners, tutorial, node
Published: 2024-06-11 13:02:07

## INTRODUCTION

Welcome to the world of server-side development. If you're new to programming or looking to dive into back-end development, this guide will walk you through setting up a Node.js server from scratch. By the end of this article, you'll have a basic understanding of Node.js, and your own basic server up and running!


### Ok but, What exactly is a server?

Imagine you want to watch a movie on Netflix. You open the app on your TV or computer, search for a movie, and press play. Behind the scenes, something crucial happens: a server starts to work.

A server is just a computer, that responds to [requests](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods) from other devices, called clients. In this scenario, when you press play, your device (the client) sends a request to Netflix’s servers. These servers store and manage vast amounts of data, including all the movies and shows available on Netflix.

Here’s a more technical breakdown:

- Client Request: Your device sends an HTTP request over the internet to Netflix’s servers, asking to stream a specific movie.
- Server Processing: The server receives this request, processes it, and fetches the required movie data from its storage.
- Data Transmission: The server then sends the movie data back to your device in a stream of data packets.
- Client Display: Your device receives the data packets and begins playing the movie for you.

Servers handle millions of these requests every second, ensuring that the right data is sent to the right device at the right time. Essentially, a server is the backbone of the internet, making sure you get the information, videos, and websites you need whenever you ask for them.

![Functioning of a server](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d7yy2ocosyn44hwgb0g5.png)

---

## Node.js

![NodeJS](https://nodejs.org/static/logos/nodejsDark.svg)


### What is Node.js?

Node.js is a powerful JavaScript runtime [What does it means?](https://dev.to/rahmanmajeed/javascript-the-runtime-environment-35a2) built on Chrome's V8 JavaScript engine. It enables developers to use JavaScript for writing command-line tools and server-side scripting. By running scripts server-side, Node.js allows the production of dynamic web page content before the page is sent to the user's web browser.

Hence, Node.js represents a "JavaScript everywhere" paradigm, unifying web-application development around a single programming language, rather than different languages for server-side and client-side scripts.


### Why Node.js?

- Single programming language (JavaScript) for both client and server side: Simplifies development and reduces context-switching.

- Non-blocking, event-driven architecture: Ideal for building data-intensive real-time applications.

- Vibrant community and rich ecosystem: Provides an abundance of freely available tools and libraries.

---

## GETTING STARTED

Install Node.js and npm:

Visit [Node.js](https://nodejs.org/) official website  and download the installer for your operating system. This will install both Node.js and npm (node package manager), as npm is the default package manager for Node.js.

### Step 1: Create a New Directory

First, we need to create a new directory (a folder) for our project and then navigate into it. In this article, we'll use the command console (also known as the command line or terminal) to do this. The command console is a text-based interface where you can type commands to perform specific tasks.

Don't worry if you're not familiar with it, I'll guide you through each step. You can use these commands on Windows, Linux, or Mac, whichever operating system you prefer. Here's how you can do it:

Open the command console:

- On Windows, you can search for "Command Prompt" or "cmd" in the start menu.
- On Mac, you can open "Terminal" from the Applications > Utilities folder.
- On Linux, you can open "Terminal" from your application menu.

Once you have the command console open, type the following commands to create a new directory and navigate into it:


```
mkdir my-first-node-server
cd my-first-node-server
mkdir my-first-node-server
cd my-first-node-server
```
### Step 2: Initialize a New Node.js Project
Now that you are inside your project directory, it's time to set up a new Node.js project. To do this, run the following command:
```
npm init -y
```
This command initializes a new Node.js project by creating a [package.json](https://dev.to/naveenchandar/package-json-file-explained-b94) file in your project directory. The -y flag automatically answers "yes" to all prompts, setting up the default configuration.

The package.json file is crucial for your project because it:

- Stores metadata about your project (such as name, version, and description).
- Lists the dependencies (libraries and modules) your project needs.
- Defines scripts to automate tasks (like starting your server).
- Helps manage versioning and ensures consistent builds across different environments.

This setup is essential for managing your project's dependencies, scripts, and versions effectively.

### Step 3: Install Express
While Node.js itself can handle HTTP requests, using a framework like Express simplifies the process. 

Install [Express](https://expressjs.com) with the following command:

```
npm install express
```

### Step 4: Create Your Server

Create a new javascript file, We'll call it server.js. You can use the touch command to create this file:

```
touch server.js
```

After creating the file, open it in your preferred text editor and add the following code:

```javascript
const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello World from my first server!');
});

const PORT = 3000;
app.listen(PORT, () => {
  console.log(`Server is running on http://localhost:${PORT}`);
});
```

This code sets up a basic server that listens on port 3000 and responds with "Hello World from my first server!" when the root URL (/) is accessed.

###Step 5: Run Your Server

Run your server using Node.js with the command:

```
node server.js
```

Open a web browser and go to http://localhost:3000. You should see the message "Hello World from my first server!" displayed.

---

Congratulations! You've just set up your first Node.js server! This basic setup can serve as a foundation for more complex applications. 

In future articles we will explore adding more routes, handling different types of requests, and integrating databases.

Don't miss out on the next steps in your backend journey and more exciting content to help you become a better developer. Stay tuned and keep coding!

<u>Thanks for reading.</u>