# 🚀 Setting Up a Basic Express Server

If you're starting out with backend development in Node.js, Express is the first framework you'll likely touch — and for good reason. It’s lightweight, unopinionated, and lets you get a server running in minutes. This guide walks you through setting up a basic Express server from scratch, with clear explanations and copy-ready code snippets.

---

## 📌 **What is Express?**

Express is a minimal and flexible Node.js web application framework that provides a robust set of features for building APIs and backend applications.

In simple words:  
➡️ *It lets you create an HTTP server quickly without dealing with Node’s low-level* `http` module.

---

# 🛠️ Step 1 — Initialize Your Project

First, create a folder and initialize a Node project:

```bash
mkdir my-express-server
cd my-express-server
npm init -y
```

This creates a `package.json` file that keeps track of your project dependencies.

---

# 📦 Step 2 — Install Express

Run:

```bash
npm install express
```

That’s it — you now have Express in your project.

---

# 📁 Step 3 — Create Your Server File

Create a file named:

```plaintext
server.js
```

or

```plaintext
index.js
```

Inside it, write:

```js
const express = require("express");
const app = express();

// Middleware to parse JSON
app.use(express.json());

// Basic route
app.get("/", (req, res) => {
  res.send("Hello from Express!");
});

// Start the server
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
  console.log(`Server running on http://localhost:${PORT}`);
});
```

---

# 🧩 Understanding the Code

### **1\. Importing Express**

```js
const express = require("express");
```

This loads the Express module.

---

### **2\. Creating the App**

```js
const app = express();
```

`app` is the main server instance. You’ll use it to define routes, middleware, etc.

---

### **3\. Middleware**

```js
app.use(express.json());
```

This allows Express to read JSON from request bodies (`req.body`).

---

### **4\. Routes**

```js
app.get("/", (req, res) => {
  res.send("Hello from Express!");
});
```

A simple GET endpoint at `/`.

---

### **5\. Starting the Server**

```js
app.listen(PORT, () => { ... });
```

Tells Express to listen for incoming requests.

---

# 🧪 Step 4 — Run the Server

In your terminal:

```bash
node server.js
```

Output:

```plaintext
Server running on http://localhost:5000
```

Open your browser → [`http://localhost:5000`](http://localhost:5000)

You should see:

```plaintext
Hello from Express!
```

---

# 📌 Common Enhancements (Optional but Useful)

## 🔥 Add Nodemon for Auto-Restart

```bash
npm install --save-dev nodemon
```

Update `package.json`:

```json
"scripts": {
  "dev": "nodemon server.js"
}
```

Run:

```bash
npm run dev
```

Now your server reloads automatically whenever you edit code.

---

## 📡 Adding Another Route

```js
app.post("/api/data", (req, res) => {
  const body = req.body;
  res.json({
    message: "Data received successfully",
    received: body,
  });
});
```

---

## ❗ Adding 404 Not Found Handler

```js
app.use((req, res) => {
  res.status(404).json({ error: "Route not found" });
});
```

> This article provides a step-by-step guide to setting up a basic Express server in Node.js. You'll learn how to initialize a project, install Express, and create a server file with routes and middleware. Additionally, it covers running your server and useful enhancements like using Nodemon for auto-restarts and handling 404 errors. Perfect for beginners looking to quickly get a server up and running.
> 
> Thank you for reading this till the end.
> 
> If you see any error,  
> please let me know by commenting or write to me on this email - abhimanyug987@gmail.com

---
