1

I have a nodejs app. Inside this app, I have a secondapp and I use vhost to use one or other. The problem is: my second app uses a node module that needs a config file (file.json). This module look for this file in the root folder. However, the module is looking at the root folder of the project (the root app), and I want that the module look at the root folder of the SecondApp.

So, the "somenodemodule" is using the "file1.json". But I want to use the "file2.json". How can I change the root folder that the "somenodemodule" will look for?

This is the code of the root app:

var express = require('express');
const app = express();
var http = require('http').Server(app);
var path = require('path');
var connect = require('connect')
var vhost = require('vhost')

var secondapp = require('./secondapp/app');

app.use(vhost('secondapp.com', secondapp));

app.get('/', function(req, res) {
    res.send('Homepage First App');
});

var porta = process.env.PORT || 3000;

http.listen(porta, function(req) {
    console.log("servidor rodando");
})

This is the code of the secondApp:

var express = require('express');
const app = express();
var http = require('http').Server(app);
var somenodemodule = require('somenodemodule');
var path = require('path');

var secondapp = somenodemodule();

app.use(secondapp);

app.get('/', function(req, res) {
    res.send('Homepage Second App');
});

module.exports = app;

And this if my folder structure:

folder structure

Community
  • 1
  • 1
  • is the config file coming from something your installed with npm or is it a file you created? – TimCodes Apr 05 '18 at 23:02
  • the module I installed has the config file ('node_modules/mymodule/config.json'). However, it is possible to override this config file. To do this, I need to create a custom config.json in the app root folder. So, the config file is a file I created – Rodrigo Francisquini Apr 05 '18 at 23:04
  • do you have 'somenodemodule' installed in both node_modules directories? – TimCodes Apr 05 '18 at 23:13
  • no, just in the "SecondApp/node_modules" directory – Rodrigo Francisquini Apr 05 '18 at 23:18
  • are you overriding the config file by passing it to some module like somemodule("path/to/config.json") or are you just providing a file in a directory with a specific name ? – TimCodes Apr 05 '18 at 23:20
  • the some module looks for a config file in the root directory. I am just providing a config file in the root directory – Rodrigo Francisquini Apr 05 '18 at 23:23
  • can you move the file2.json from the second app directory to the root directory? – TimCodes Apr 05 '18 at 23:25
  • if not one hakcy thing you could do is open up the code in somenodemodule and see where the module is consuming the config file and change the path there – TimCodes Apr 05 '18 at 23:25
  • yes, I can. But this is not the right solution for my case... I want to have a third app, and this third app will have a "file3.json"... so, for every app, I will have a different config file... – Rodrigo Francisquini Apr 05 '18 at 23:27
  • I could change the path inside the somenodemodule file. But I'd like to found a solution that does not change the module code. I want my app working with after "npm install". – Rodrigo Francisquini Apr 05 '18 at 23:29
  • nested node_modules is tricky business. Not sure you can config vhost to choose the root dir.. you might try and use NGINX instead of Vhost – TimCodes Apr 05 '18 at 23:36
  • https://github.com/nodejs/node-v0.x-archive/issues/6960 – TimCodes Apr 05 '18 at 23:37
  • https://stackoverflow.com/questions/11570321/configure-nginx-with-multiple-locations-with-different-root-folders-on-subdomain – TimCodes Apr 05 '18 at 23:37
  • this app will be deployed to AWS Elastic Beanstalk. I was trying to avoid changing nginx config. But it seems that this is the only way... – Rodrigo Francisquini Apr 05 '18 at 23:38
  • Might be one but not one i know of, good luck though :) – TimCodes Apr 05 '18 at 23:42

0 Answers0