devnotes.

Create routes using Express and Postman

Postman makes it easy to exercise every route you build in Express without writing a frontend first. Here's a walkthrough for a small inventory service.

Prerequisites

  • Node.js and npm
  • Express
  • Postman installed

Step 1: Set up the project

mkdir inventory-service
cd inventory-service
npm init -y
npm install express

Step 2: Create the server file

touch server.js

Step 3: Write the routes

// server.js
const express = require("express");
const app = express();
const PORT = 5000;

app.use(express.json());

app.get("/", (req, res) => {
    console.log("GET request handled");
    res.send("Inventory Service is running");
});

app.post("/items", (req, res) => {
    console.log("POST request handled", req.body);
    res.send("Item created");
});

app.put("/items/:id", (req, res) => {
    console.log("PUT request handled", req.body);
    res.send(`Item ${req.params.id} updated`);
});

app.delete("/items/:id", (req, res) => {
    console.log("DELETE request handled");
    res.send(`Item ${req.params.id} deleted`);
});

app.listen(PORT, () => {
    console.log(`Server listening on ${PORT}`);
});

Step 4: Run the server

node server.js

Step 5: Test each route in Postman

MethodEndpointPurpose
GETlocalhost:5000/Health check
POSTlocalhost:5000/itemsCreate an item
PUTlocalhost:5000/items/1Update item with id 1
DELETElocalhost:5000/items/1Delete item with id 1

For POST and PUT, set the request body to raw JSON, e.g. { "name": "Notebook", "qty": 10 }, and Postman will show the response text your server sends back.

Create routes using Express and Postman | DevNotes