Close Menu
Soshace Digital Blog

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    What's Hot
    Beginners

    Sorting Algorithms Overview: Theory and Visualization

    Beginners

    Deep Learning vs Machine Learning: Overview & Comparison

    Beginners

    7 Concepts: Why Your SVG Is the Way It Is

    Important Pages:
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Today's Picks:
    • Scaling Success: Monitoring Indexation of Programmatic SEO Content
    • Leveraging Influencers: Key Drivers in New Product Launches
    • How Privacy-First Marketing Will Transform the Industry Landscape
    • The Impact of Social Proof on Thought Leadership Marketing
    • Balancing Value-Driven Content and Promotional Messaging Strategies
    • Top Influencer Marketing Platforms to Explore in 2025
    • Emerging Trends in Marketing Automation and AI Tools for 2023
    • Strategies to Mitigate Duplicate Content in Programmatic SEO
    Sunday, September 28
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Soshace Digital Blog
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Services
    • SaaS & Tech

      Maximizing Efficiency: How SaaS Lowers IT Infrastructure Costs

      August 27, 2025

      Navigating Tomorrow: Innovations Shaping the Future of SaaS

      August 27, 2025

      Maximizing Impact: Strategies for SaaS & Technology Marketing

      August 27, 2025
    • AI & Automation

      Enhancing Customer Feedback Analysis Through AI Innovations

      August 27, 2025

      Navigating the Impact of AI on SEO and Search Rankings

      August 27, 2025

      5 Automation Hacks Every Home Service Business Needs to Know

      May 3, 2025
    • Finance & Fintech

      Critical Missteps in Finance Marketing: What to Avoid

      August 27, 2025

      Analyzing Future Fintech Marketing Trends: Insights Ahead

      August 27, 2025

      Navigating the Complex Landscape of Finance and Fintech Marketing

      August 27, 2025
    • Legal & Compliance

      Exploring Thought Leadership’s Impact on Legal Marketing

      August 27, 2025

      Maximizing LinkedIn: Strategies for Legal and Compliance Marketing

      August 27, 2025

      Why Transparency Matters in Legal Advertising Practices

      August 27, 2025
    • Medical Marketing

      Enhancing Online Reputation Management in Hospitals: A Guide

      August 27, 2025

      Analyzing Emerging Trends in Health and Medical Marketing

      August 27, 2025

      Exploring Innovative Content Ideas for Wellness Blogs and Clinics

      August 27, 2025
    • E-commerce & Retail

      Strategic Seasonal Campaign Concepts for Online and Retail Markets

      August 27, 2025

      Emerging Trends in E-commerce and Retail Marketing Strategies

      August 27, 2025

      Maximizing Revenue: The Advantages of Affiliate Marketing for E-Commerce

      August 27, 2025
    • Influencer & Community

      Leveraging Influencers: Key Drivers in New Product Launches

      August 27, 2025

      Top Influencer Marketing Platforms to Explore in 2025

      August 27, 2025

      Key Strategies for Successful Influencer Partnership Negotiations

      August 27, 2025
    • Content & Leadership

      The Impact of Social Proof on Thought Leadership Marketing

      August 27, 2025

      Balancing Value-Driven Content and Promotional Messaging Strategies

      August 27, 2025

      Analyzing Storytelling’s Impact on Content Marketing Effectiveness

      August 27, 2025
    • SEO & Analytics

      Scaling Success: Monitoring Indexation of Programmatic SEO Content

      August 27, 2025

      Strategies to Mitigate Duplicate Content in Programmatic SEO

      August 27, 2025

      Effective Data Visualization Techniques for SEO Reporting

      August 27, 2025
    • Marketing Trends

      How Privacy-First Marketing Will Transform the Industry Landscape

      August 27, 2025

      Emerging Trends in Marketing Automation and AI Tools for 2023

      August 27, 2025

      Maximizing ROI: Key Trends in Paid Social Advertising

      August 27, 2025
    Soshace Digital Blog
    Blog / JavaScript / React / Creating Mock API using Mirage in a React application
    React

    Creating Mock API using Mirage in a React application

    Vivek BishtBy Vivek BishtAugust 12, 2020No Comments9 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    Creating Mock API using Mirage in a React application
    Creating Mock API using Mirage in a React application
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link
    Creating Mock API using Mirage in a React application
    Creating Mock API using Mirage in a React application

    Creating independent front-ends for any application leads to easily maintainable and flexible code but, for creating such independent front-ends, one has to mock the complete API so that the front-end has data and handles all the requests. The process of mocking the API and data can get tedious. Using API mocking libraries can help reduce the time of creating a full-fledged front-end that does not depend on any back-end services.

    One such API mocking library is Mirage. In this article, we are going to use Mirage and create a full-fledged React application that does not rely on any back-end service. We are going to create a REST API for the following application:

    Image of the application we are going to create
    Image of the application we are going to create

    It is a book store application where we can add books along with the genre of the books. It also displays our stored book list. The book list is displayed based on two categories, one is the complete list of books stored, the other is the list of books based on genre.

    I have provided the link to the starter code which includes all the basic setup that is required for this application:

    https://github.com/Bviveksingh/mirage-starter-code

    After cloning or downloading the starter-code, run `npm install` in the terminal to install all the dependencies. After installing the dependencies, run the application using `npm start`command. This is how the application look with the starter code:

    Image of the application with the starter code
    Image of the application with the starter code

    Try clicking on the show all books button, you should see an error in the console, Mirage error. This is because our application is currently sending HTTP requests to an endpoint which we haven’t coded yet. So let’s start building the API,

    We have to install Mirage to our application first, so run the following command to install mirage:

    npm i --save-dev miragejs

    Next, inside the components folder, create a file named server.js and add the following code inside the file:

    import { createServer } from "miragejs"
    
    export default function () {
      createServer()
    }
    

    The createServer( ) function creates the server for us. All the routes,models and mock data will go inside this function.  If you want to use the Mirage server only during the development phase, inside the index.js file, add the following code:

    import createServer from "./server";
    
    if (process.env.NODE_ENV === "development") {
      createServer(); 
    }
    

    By writing the above code, the mirage server will run only in the development phase.

    Now that we have completed setting up the Mirage server, let’s move onto creating models for the API. We are going to create two models, one for storing books and the other for storing genres.

    Inside the createServer function, add the following code:

    createServer({
        models:{
          book:Model,
          genre:Model
        }
      })
    

    The models property inside the createServer function specifies the number of Models this server consists of. When using POST requests, Mirage uses it’s in-memory database to store our application data.

    **Note– Don’t forget to import Model from miragejs.

    Now that we have created models, let’s move on to creating endpoints for these models. For this application we are going to use the following endpoints:

    Inside the createServer function, add another property (a method in this case) called routes( ). This method will contain all the endpoints required for the application.

    createServer({
        models:{
          book:Model,
          genre:Model
        },
        routes(){
          
        }
     })
    

    All the endpoints should be written inside the routes function. Let’s add the first endpoint for creating a book:

     this.post('/api/add_book',(schema,request)=>{
          let body = JSON.parse(request.requestBody);
          return schema.books.create(body);
     });
    

    Just like creating endpoints for REST API, the above POST request takes in two parameters, first one is the name of the endpoint and the second is a callback function. The callback function has two parameters: schema (for accessing the data layer of Mirage) and request (to access the request body and params). We can access the book Model using schema.books, for creating new data we use the create method.

    Read More:  Create simple POS with React, Node and MongoDB #6: Redux Integration

    Mirage automatically creates documents with incrementing ids so we don’t have to worry about each document having a unique id. For example, consider adding 3 books to the database, this is how they will be represented:

    name:"Book1", id:"1"
    name:"Book2",id:"2"
    name:"Book3", id:"3"

    Now run the application again, try adding a new book by clicking the add book button. You should see 201 status code for the Mirage request inside the console window. Next, for getting the list of all books stored along with deleting a specific book add the following endpoints:

    this.get('/api/books',(schema)=>{
        return schema.books.all();
    });
    
    this.delete('/api/book/:id',(schema,request)=>{
        let id = request.params.id;
        return schema.books.find(id).destroy();
    });
    

    The above two endpoints are pretty straight forward, the GET request endpoint returns the complete list of books from the in-memory database using the all( ) method. Then, the DELETE request endpoint is a dynamic endpoint, which takes an id as the parameter, inside the callback function,we use the destroy( ) method to delete the document from the database.

    To check whether the application is working, try adding a new book and then click the show all books button, you should see the list of books appearing inside the books section:

    GIF displaying the add-book feature
    GIF displaying the add-book feature

    The cover page of all the books will be a static image which we are loading from the images folder. You can go ahead and change it. As you can see we are now able to add a new book to the Mirage database. Next, for checking the DELETE endpoint, try deleting the a book from the list by clicking the delete button on the book item. After clicking the book item, click on the show all books button again you should no longer see the book you deleted.

    There are two other end points that we need to add to our server, the GET all genres endpoint and the GET all books of specific genre endpoint:

    this.get('/api/genres',(schema,request)=>{
            return schema.genres.all();
    });
    

    ****We are going to add the GET all books of specific genre end-point when we discuss relational databases.

    So right now, our application works and we are able to add/get/delete books from the Mirage database, the point here to notice is that every time we reload the application, we receive an empty list of books. This is because the Mirage server reruns when the application gets reloaded and therefore the list is empty.What if we want to load some initial mock data so that our book list is not empty when we start the application? For doing so, Mirage provides a method called seeds inside of which we add all the initial mock data which can be used in the application.

    Other than loading initial books, if you observe the application, when clicking on the Group by Genre button, we don’t see any list appearing. The list contains all the genres that have been created in the database but, we have not added any genre to the database yet.

    Read More:  Creating Our Own Chat GPT

    So let’s add some initial books and genres to the database. Add the following method inside the createServer function:

    seeds(server){
         server.create("genre",{name:"Mystery"});
         server.create("genre",{name:"Thriller"});
         server.create("genre",{name:"Sci-fi"});
    
         server.create("book",{name:"Book1"});
         server.create("book",{name:"Book2"});
         server.create("book",{name:"Book3"});
         server.create("book",{name:"Book4"});
    }
    

    As one can see in the code above, using the server.create method, we create both initial books and genres. Now when you run the application and click the show all books button, you should see all the 4 books appearing in the list. Along with this, you can also see the list of genres by clicking the group by genre button:

    Group by Genre list gets displayed
    Group by Genre list gets displayed

    So we have completed the basic operations that are performed in the back-end but, we can see that nothing really happens when you click on one of the items inside the Group by Genre list. This is where relational data comes into picture and we’re going to learn that next.

    We have created models that are not related to each other. What if we want the models to have some kind of relationship? Mirage provides a way to add relationships between models. Let’s take a look at how that is possible.

    We will create a one-to-many relationship between the book and genre model i.e each genre can be related to many books and every book is related to one genre. This way we are creating a one-to-many relationship. Let’s write code for that. Inside the models property of the createServer function, make the following changes:

    models:{
      book:Model.extend({
         genre:belongsTo()
       }),
      genre:Model.extend({
         books:hasMany()
       })
    }

    In the above code, by adding belongsTo and hasMany methods, we are specifying the server that the book and genre models maintain a one-to-many relationship. Now the server knows that book.genre and genre.books properties are relationships between the models.

    **Note- Don’t forget to import belongsTo and hasMany methods from ‘miragejs’

    Now that we have specified relationships, let’s add some initial mock data:

    let mysteryGenre = server.create("genre",{name:"Mystery"});
    let thrillerGenre = server.create("genre",{name:"Thriller"});
    let sciFiGenre = server.create("genre",{name:"Sci-fi"});
    
    server.create("book", {name:"Book5", genre:mysteryGenre});
    server.create("book",{name:"Book6",genre:thrillerGenre});
    server.create("book",{name:"Book7",genre:sciFiGenre});
    

    Until now, we have created books only using the name property but as one can see in the above code, we are now creating new books having the genre property attached to it. Mirage understands that this property is a relationship and hence all the books from now will be saved with the following properties in the database:

    name:"Book5",id:"4", genreId:"1"

    We can now add the end-point for displaying all the books related to a particular genre:

    this.get('/api/genre/:id/books',(schema,request)=>{
       let genreId = request.params.id;
       return schema.genres.find(genreId).books;
    });

    In the above end-point we are first searching for the genre with the particular id and then finding all books related to that genre. Now, try clicking on one of the items inside the Group by Genre list you should the specific book related to that genre appear in the books section:

    GIF displaying the group-by-genre feature
    GIF displaying the group-by-genre feature

    So there you go, we have created an API which loads initial data, contains endpoints, performs most of the CRUD operations and also contains models. Did you see how easy it was to create a fully independent front-end application? Go ahead and try creating some awesome independent front-ends.

    For the live demo, checkout the link below:

    https://codesandbox.io/s/mirage-react-o7j6l?file=/src/App.js

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Vivek Bisht

      Related Posts

      JAMstack Architecture with Next.js

      March 15, 2024

      Rendering Patterns: Static and Dynamic Rendering in Nextjs

      March 7, 2024

      Handling Mutations and Data Fetching Using React Query

      September 12, 2023
      Leave A Reply Cancel Reply

      You must be logged in to post a comment.

      Stay In Touch
      • Facebook
      • Twitter
      • Pinterest
      • Instagram
      • YouTube
      • Vimeo
      Don't Miss
      B2B Leads December 16, 2024

      Effective Strategies for B2B Lead Generation with Paid Ads

      Unlocking B2B lead generation with paid ads requires a strategic approach. Start by defining your target audience, utilizing platforms like LinkedIn for precision. A/B testing creative and messaging can optimize ROI, ensuring your ads resonate and convert.

      Crafting a High-Performing Team: A Startup’s Essential Guide

      December 16, 2024

      Swarm Intelligence: Intensities

      April 23, 2023

      Mastering Phone Interviews: Strategies for Success

      November 25, 2024

      Categories

      • AI & Automation
      • Angular
      • ASP.NET
      • AWS
      • B2B Leads
      • Beginners
      • Blogs
      • Business Growth
      • Case Studies
      • Comics
      • Consultation
      • Content & Leadership
      • CSS
      • Development
      • Django
      • E-commerce & Retail
      • Entrepreneurs
      • Entrepreneurship
      • Events
      • Express.js
      • Facebook Ads
      • Finance & Fintech
      • Flask
      • Flutter
      • Franchising
      • Funnel Strategy
      • Git
      • GraphQL
      • Home Services Marketing
      • Influencer & Community
      • Interview
      • Java
      • Java Spring
      • JavaScript
      • Job
      • Laravel
      • Lead Generation
      • Legal & Compliance
      • LinkedIn
      • Machine Learning
      • Marketing Trends
      • Medical Marketing
      • MSP Lead Generation
      • MSP Marketing
      • NestJS
      • Next.js
      • Node.js
      • Node.js Lessons
      • Paid Advertising
      • PHP
      • Podcasts
      • POS Tutorial
      • Programming
      • Programming
      • Python
      • React
      • React Lessons
      • React Native
      • React Native Lessons
      • Recruitment
      • Remote Job
      • SaaS & Tech
      • SEO & Analytics
      • Soshace
      • Startups
      • Swarm Intelligence
      • Tips
      • Trends
      • Vue
      • Wiki
      • WordPress
      Top Posts

      Conducting Effective Post-Project Evaluations: A Guide

      JavaScript December 16, 2024

      Maximizing Lead Generation with LinkedIn InMail Strategies

      LinkedIn December 6, 2024

      8. Уроки Node.js. Наследование от ошибок Error

      Programming September 12, 2016

      Advanced Mapmaking: Using d3, d3-scale and d3-zoom With Changing Data to Create Sophisticated Maps

      JavaScript March 11, 2020

      Subscribe to Updates

      Get The Latest News, Updates, And Amazing Offers

      About Us
      About Us

      Soshace Digital delivers comprehensive web design and development solutions tailored to your business objectives. Your website will be meticulously designed and developed by our team of seasoned professionals, who combine creative expertise with technical excellence to transform your vision into a high-impact, user-centric digital experience that elevates your brand and drives measurable results.

      7901 4th St N, Suite 28690
      Saint Petersburg, FL 33702-4305
      Phone: 1(877)SOSHACE

      Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
      Our Picks
      Startups

      Safeguarding Innovation: A Guide to Startup Intellectual Property

      Blogs

      Tech Blogging for Web Developers in 2019: Why? Which Platform? How to Start Your Own Tech Blog?

      Entrepreneurship

      Navigating Business Failures: Strategies for Growth and Learning

      Most Popular

      Programming Patterns. Publisher/Subscriber, Mediator

      Programming

      6 Reasons to Integrate AI into Software Testing

      Beginners

      Full List of JavaScript Conferences 2020 [41 Events] Updated 28.08.2020

      Events
      © 2025 Soshace Digital.
      • Home
      • About
      • Services
      • Contact Us
      • Privacy Policy
      • Terms & Conditions

      Type above and press Enter to search. Press Esc to cancel.