# REST API Without the Mess (No DB, No Stress)

### Context :

Welcome to the chill zone of backend development. In this blog, we’ll build a fully functional REST API — **without touching a single database**. That’s right, no MongoDB, no MySQL, not even a lonely JSON file.

We’ll use **in-memory dummy data** to understand how APIs really work — focusing on routes, methods, status codes, and the overall flow. It’s the perfect starting point before diving into real-world backend madness.

So grab your Postman and let’s ship some fake data like pros.

---

### Pre-requisite :

In the previous blog I have discussed about all the jargons of REST API. Please check out that . After that We will continue with this project .

here is the [setup](https://github.com/IamBATMaN420/api-bolier-plate) guide for the project . Using ( node express and nodemon )

---

## Code

* ### **Making basic server :**
    

Let’s Jump to code directly . Grab your coffee and let’s start

```javascript
import express from "express"

const app = express();

app.get("/api", (req, res) => {
  res.send("helllo it's working!")
});

app.listen(3000, () => {
  console.log(`server is running on port 3000`)
})
```

* This kick start the server
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1749228767006/d18c1773-4c56-4992-9f43-426c1c3d2527.png align="center")

---

* ### **Making dummy data** :
    

Let’s hard code the dummy data to reduce the complexity and understand

```javascript
const products = [
  {
    name: "Laptop",
    price: 49999.99,
    quantity: 1,
    active: true
  },
  {
    name: "Wireless Mouse",
    price: 799.00,
    quantity: 2,
    active: true
  },
  {
    name: "Mechanical Keyboard",
    price: 2999.50,
    quantity: 1,
    active: false
  },
  {
    name: "27-inch Monitor",
    price: 13499.00,
    quantity: 1,
    active: true
  },
  {
    name: "USB-C Hub",
    price: 1499.00,
    quantity: 3,
    active: true
  },
  {
    name: "Noise Cancelling Headphones",
    price: 8999.99,
    quantity: 1,
    active: false
  },
  {
    name: "Webcam",
    price: 2199.00,
    quantity: 2,
    active: true
  },
  {
    name: "Gaming Chair",
    price: 12499.00,
    quantity: 1,
    active: true
  }
];
// dummy data
```

---

* ### **Reading all the product** :
    

Reading the data from Product i.e. the dummy data . With `get` method

```javascript
app.get("/api/products", (req, res) => {
  res.json(products);
})
// reading all product
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1749230388294/8b3df81e-6447-40d1-aeca-6cbd53b1d753.png align="center")

So, we are passing the JSON data with named “ product “ directly … We are sending the response → JSON type .

---

* ### Creating the product :
    

As We know for creating products we have `post` http method .

```javascript
app.post("/api/products", (req, res) => {
  console.log(req.body)
  res.send("Okay")
})
// crateing product
```

here in the code , We are logging the `req.body` that have the JSON data we are sending through [postman](https://www.postman.com/) … Yes can't check the output in the browser. We have to send the req from postman and then from we will get the res ( with status code ).

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1749231072205/67062590-bacc-4bbb-96be-c4804d581b9f.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1749231089241/b2037c48-fda4-4989-a4db-70baf56c4b73.png align="center")

* So here we are using postman to send the `JSON` data and we are seeing that in postman that’s fine but in VS code it’s giving `undefined` .
    
* The reason is we can't parse JSON data without any global middle-ware. So , We are using
    
    `app.use(express.json())` . It will parse the data and now we can see it.
    

---

### Update and Delete a product by ID →

```javascript
app.put("/api/products/:id", (req, res) => {
  const product = products.find(product => product.id == req.params.id)
  if (!product) {
    res.status(404).json({
      "message": "WTH go away"
    })
  }

  const { name, price, quantity, active } = req.body;
  if (!name) {
    res.json({
      "msssage": "Nah hain bhai",
    })
  }
// used previously 
  if (name) {
    product.name = name
  }
  if (price) {
    product.price = price
  }
  if (quantity) {
    product.quantity = quantity
  }
  if ("active" in req.body) {
    product.active = active
  }
  res.status(201).json({
    "messgae": "Update is workingg"
  })
})

app.delete("/api/products/:id", (req, res) => {
  const productIndex = products.find(productIndex => productIndex.id == req.params.id)
  if (productIndex == -1) {
    res.status(404).json({
      "messgae": "hey unexpected backchodi!"
    })
  }

  products.splice(productIndex, 1)
  res.status(201).json({
    "message": "Delete is working!!!"
  })

})
```

In this code, We are using `splice` and `find` … function . And In every routes we are using dynamic parameter `:id`. Moreover, what the thing that is after `base_url/xyz` → here `xyz` is parameter.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1749231405510/fc6470d1-5217-4c18-b4d7-0ce6462bbd49.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1749231445928/ab951399-d2af-4f0c-a731-a8d81939e74b.png align="center")

### here is the full raw code.

```javascript
import express from "express"
import crypto from "crypto";

const app = express();

app.use(express.json())

const products = [  {
    id: "0bf735e2-fb4a-445a-9dcd-7b672ce62993",
    name: "Laptop",
    price: 49999.99,
    quantity: 1,
    active: true
  },
  {
    id: "0bf735e2-fb4a-445a-9dcd-7b672ce62994",
    name: "Wireless Mouse",
    price: 799.00,
    quantity: 2,
    active: true
  },
  {
    id: "0bf735e2-fb4a-445a-9dcd-7b672ce62997",
    name: "Mechanical Keyboard",
    price: 2999.50,
    quantity: 1,
    active: false
  },
  {
    id: "0bf735e2-fb4a-445a-9dcd-7b672ce62999",
    name: "27-inch Monitor",
    price: 13499.00,
    quantity: 1,
    active: true
  },
  {
    id: "0bf735e2-fb4a-445a-9dcd-7b672ce62990",
    name: "USB-C Hub",
    price: 1499.00,
    quantity: 3,
    active: true
  },
  {
    id: "0bf735e2-fb4a-445a-9dcd-7b672ce62995",
    name: "Noise Cancelling Headphones",
    price: 8999.99,
    quantity: 1,
    active: false
  },
  {
    id: "0bf735e2-fb4a-445a-9dcd-7b672ce629988",
    name: "Webcam",
    price: 2199.00,
    quantity: 2,
    active: true
  },
  {
    id: "0bf735e2-fb4a-445a-9dcd-7b672ce629977",
    name: "Gaming Chair",
    price: 12499.00,
    quantity: 1,
    active: true
  }
];
// dummy data

app.get("/api", (req, res) => {
  res.send("helllo it's working!1")
});

app.get("/api/products", (req, res) => {

  res.json(products);
})
// reading all product


app.post("/api/products", (req, res) => {

  const { name, price, quantity, active } = req.body;
  if (!name) {
    res.status(422).json(
      { "message": "nahi honga bhai" }
    )
  }
  const id = crypto.randomUUID()
  products.push({ id, name, price, quantity, active })
  res.status(201).json({ "masage": "product is added", id })

})
// crateing product



app.get("/api/products/:id", (req, res) => {
  const product = products.find(product => product.id == req.params.id)
  if (!product) {
    res.status(404).json({
      "message": "WTH go away"
    })
  }
  res.status(201).json(product)
})
// get a product by :id

app.put("/api/products/:id", (req, res) => {
  const product = products.find(product => product.id == req.params.id)
  if (!product) {
    res.status(404).json({
      "message": "WTH go away"
    })
  }

  const { name, price, quantity, active } = req.body;
  if (!name) {
    res.json({
      "msssage": "Nah hain bhai",
    })
  }
  if (name) {
    product.name = name
  }
  if (price) {
    product.price = price
  }
  if (quantity) {
    product.quantity = quantity
  }
  if ("active" in req.body) {
    product.active = active
  }
  res.status(201).json({
    "messgae": "Update is workingg"
  })
})
// update data 

app.delete("/api/products/:id", (req, res) => {
  const productIndex = products.find(productIndex => productIndex.id == req.params.id)
  if (productIndex == -1) {
    res.status(404).json({
      "messgae": "hey unexpected backchodi!"
    })
  }

  products.splice(productIndex, 1)
  res.status(201).json({
    "message": "Delete is working!!!"
  })

})
// delteing data

app.listen(3010, () => {
  console.log(`server is running on port 3010`)
})
```

---

### Conclusion :

We’ve nailed all the CRUD operations using REST API — no database, no stress! Now, let’s clean up our code and organize it better with a proper folder structure.

Check out the GitHub repo for hands-on practice:  
🔗 [GitHub Repo](https://github.com/IamBATMaN420/api-no-db)

Next up: cleaner code, modular files, and pro-level structure. Let’s build better!

---
