1

Im using a lot of API's whose adapters includes a lot of event listeners and I seek suggestions on how to improve the structure of the code, especially in regards of moving code out form app.js to another destination (file). Do you guys have any tips on how to do so?

Code example (app.js):

import express from 'express';

// Dot Environment
const dotenv = require('dotenv');

// See if we can fetch the config
const result = dotenv.config()

// Ensure early that we can load the environment variables
if (result.error) throw result.error

// Example APIs
const { Api1 } = require('api1');
const { Api2 } = require('api2');
const { Api3 } = require('api2');

// Slack Adapter
const api1Events = Api1('x');
const api2Events = Api2('z');
const api3Events = Api3('y');

const request = require('request');

// Express
const app = express();

// Middleware
app.use('/api1/events', api1Events.expressMiddleware());
app.use('/api2/events', api2Events.expressMiddleware());
app.use('/api3/events', api3Events.expressMiddleware());

// Event listeners
api1Events.on('something', (data) => func1(data));
api2Events.on('something', (data) => func2(data));
api3Events.on('something', (data) => func3(data));

function func1(data) {
  // Image 1000 lines below calling tons of other functions
}

function func2(data) {
  // Image 1000 lines below calling tons of other functions
}

function func3(data) {
  // Image 1000 lines below calling tons of other functions
}

// Listener
app.listen(3000, () => console.log('Example app listening on port 3000!'));
robtot
  • 863
  • 1
  • 9
  • 31
  • 1
    Not sure why this was marked as a duplicate since the linked question seems almost entirely unrelated. – Jordan Running Mar 25 '19 at 16:08
  • @jordan I partially agree, however this question is still too broad then. The duplicate provides some hints into the right direction. – Jonas Wilms Mar 25 '19 at 16:09
  • 1
    @JonasWilms If it's too broad you should mark it as "Too Broad" and then link to that other question in a comment. It's not a duplicate. – Jordan Running Mar 25 '19 at 16:11
  • The topic that you marked as duplicate does not unfortunately answer my questions. I simply wish to know if I can move events.on from the app.js to another file. If so, how? Thanks – robtot Mar 26 '19 at 09:09

0 Answers0