Steps to create Express Application and Postman testing

Step 1: Create a directory in which the express application will store

mkdir express-postman
cd express-postman

Step 2: Initialize the project with the following command

npm init -y

Step 3: Install the required dependencies.

npm i express nodemon mongoose

Project Structure:

Folder Structure

The updated dependencies in package.json file will look like:

"dependencies": {
"express": "^4.18.2",
"mongoose": "^8.0.3",
"nodemon": "^3.0.2"
}

Example: Creating a simple Express application for POST and GET userData.

Javascript




//app.js
 
const express = require("express");
const mongoose = require("mongoose");
 
const app = express();
app.use(express.json());
 
// replace the connection string with your MongoDB URI.
mongoose.connect(
    "Your connection string",
    {
        useNewUrlParser: true,
        useUnifiedTopology: true,
    }
);
 
// Create a Mongoose schema for users
const userSchema = new mongoose.Schema({
    name: String,
    email: String,
    mobile: Number,
});
 
const User = mongoose.model("Userdata", userSchema);
 
app.get("/", (req, res) => {
    res.send("API testing");
});
 
app.post("/users", async (req, res) => {
    try {
        const { name, email, mobile } = req.body;
        const newUser = new User({ name, email, mobile });
        await newUser.save();
        res.status(201).json(newUser);
    } catch (error) {
        res.status(500).json({ message: error.message });
    }
});
 
app.get("/users", async (req, res) => {
    try {
        const users = await User.find();
        res.json(users);
    } catch (error) {
        res.status(500).json({ message: error.message });
    }
});
 
app.listen(3000, () => {
    console.log("App is running on port 3000");
});


Steps to run the application:

nodemon app.js

Output:

Browser Output.

How to use postman for testing express application

Testing an Express app is very important to ensure its capability and reliability in different use cases. There are many options available like Thunder client, PAW, etc but we will use Postman here for the testing of the Express application. It provides a great user interface and numerous tools which makes API testing very easy.

Postman is an API(utility programming interface) development device that enables construction, takes a look at, and alters APIs. It could make numerous varieties of HTTP requests(GET, POST, PUT, PATCH), store environments for later use, and convert the API to code for various languages(like JavaScript, and Python). In this article, we will learn How to use postman for testing express application

Similar Reads

Prerequisites:

Node and NPM/Yarn installed. Knowledge of Node JS and Express JS Understanding of APIs. Overview of Postman...

Steps to create Express Application and Postman testing

Step 1: Create a directory in which the express application will store...

API testing with Postman

...

Contact Us