Aim: To build the first part of the Fullstack Paytm application backend.

Methodology

The task is to build a Paytm clone, frontend and backend.

1. Mongoose Schema /backend/db.js

Here there are certain things that I have known earlier and new things that I have learnt. To install mongoose, run npm install mongoose.

There are mainly three things to keep in mind while playing with Mongoose and MongoDB,

const mongoose = require('mongoose');
const { minLength, maxLength } = require('zod/v4');
const { required } = require('zod/v4-mini');

mongoose.connect('mongodb+srv://venomc381:Venom%[email protected]/');

const userSchema = mongoose.Schema({
    firstname: {
        type: String,
        required: true,
        unique: true,
        trim: true,
        minLength: 5,
        maxLength: 30
    },
    lastname: {
        type: String,
        required: true,
        minLength: 3,
        trim: true,
        maxLength: 20
    },
    username: {
        type: String,
        required: true,
        minLength: 4,
        trim: true
    },
    password: {
        type: String,
        trim: true,
        required: true,
        minLength: 8
    },
});

const User = mongoose.model('Paytm-users', userSchema);

module.exports = {
    User
};

In the above code, I have made the schema entirely and I have also added more things on top of it which I learnt from harkirat’s elegant solution.

I added qualities of the characteristics for example, the password has to be a string with a minimum length of eight and is required, this can also be done in zod. You need to decide where to do this, in zod or mongoose. Select one.

2. Creating Routes Very Important

Express.Router() is a feature in Express.js that helps organize routes into separate files or modules. Instead of defining all routes in a single app.js file, you can group related routes using Router (like user routes, product routes, etc.). This makes your code modular, cleaner, and easier to maintain.

Here is a simple example,

// userRoutes.js
const express = require('express');
const router = express.Router();

router.get('/profile', (req, res) => {
  res.send('User Profile');
});

module.exports = router;