question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there any way to modularize routes?

See original GitHub issue

Hello!! I am using mirageJs into my react native app to mock some externals apis calls in development. In this moment i have one big index.ts file, handling a lot of request from multiple apis, but that is not ideal. There is any way to build the routes with many files, like routes/external_api1.ts, and after import then in my server routes? Or there is any another way to do anything like that?

My index.ts that i want to split in api_auth and api_cep files, for example:

import { Server, Response } from 'miragejs';

export default function ({ environment = 'development' } = {}) {
  return new Server({
    environment,

    routes() {
      this.post('api_auth/signup', () => {
        return new Response(201, {}, userAuthResponse);
      });

      this.post('api_auth/login', () => {
        return new Response(201, {}, userAuthResponse);
      });

      this.post('api_cep/login', () => {
        return new Response(201, {}, cepResponse);
      });
    }

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
samselikoffcommented, Oct 21, 2020

There’s a few ways you could do this – maybe after you find something you like we can add it to the docs!

First you could import functions and call them in your routes() hook:

import { createServer } from 'miragejs';
import apiAuthHandlers from './handlers/api-auth';
import apiCepHandlers from './handlers/api-cep';

export default function ({ environment = 'development' } = {}) {
  return createServer({
    environment,

    routes() {
      apiAuthHandlers(this)
      apiCepHandlers(this)
    }
  }
})

These could just be functions that take in the server instance (which is this within routes()) and define the handlers there:

// /mirage/handlers/api-auth.js
export default function(server) {
  server.post('api_auth/signup', () => {
    return new Response(201, {}, userAuthResponse);
  });

  server.post('api_auth/login', () => {
    return new Response(201, {}, userAuthResponse);
  }); 
}

I’ve done this before and it works great.

Alternatively you might be able to use the config() method on a server instance, which lets you augment an existing server with additional config. That could look something like this:

import { createServer } from 'miragejs';
import addApiAuthHandlers from './handlers/api-auth';
import addApiCepHandlers from './handlers/api-cep';

export default function ({ environment = 'development' } = {}) {
  let server = createServer({ environment });

  addApiAuthHandlers(server)
  addApiCepHandlers(server)

  return server;
})

Then those functions would look something like this:

// mirage/handlers/api-auth

export default function(server) {
  server.config({
    routes() {
      this.post('api_auth/signup', () => {
        return new Response(201, {}, userAuthResponse);
      });

      this.post('api_auth/login', () => {
        return new Response(201, {}, userAuthResponse);
      });
    }
  })
}

The nice thing about this approach is that because the other hooks are also available in the config() method, you could co-locate some related logic in these files, like adding more models or seeds to your server.

Let me know if one of those approaches works out!

2reactions
TimRChencommented, Mar 19, 2021

It’s great. But why not put this practice in the official document ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to modularize routing with Node.js Express
How to modularize routing with Node. js Express - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.
Read more >
Routes in Node.js | Writing modularized codes using routes
Routes in node.js are used to accept HTTP requests and respond to the requests. Each route defines an unique URI in the application...
Read more >
How to modularize and re-use the various modules using Sub ...
Using the SAP Routing and Navigation in manifest.json, you can manage the routing throughout the Application using sub-routes. Routing Pattern.
Read more >
How to make nodejs applications modular - Simple Engineering
Modularization of expressjs route, as for any other module forming procedure, requires 2 steps at a minimum. The first step is to make...
Read more >
express-modularity - npm
With express-modularity, you can create multiple route files within a predetermined directory of your application and easily include global ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found