Is there any way to modularize routes?
See original GitHub issueHello!! 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:
- Created 3 years ago
- Reactions:1
- Comments:14 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
These could just be functions that take in the server instance (which is
this
withinroutes()
) and define the handlers there: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:Then those functions would look something like this:
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!
It’s great. But why not put this practice in the official document ?