0

I have two websites, websiteEN and websiteDE. They are both hosted on EC2 on their own instance. their urls are : website.en and website.de they use their own database websiteENDB and websiteDEDB.

I a currently using environment variables on AWS for their database.

To save money, I would like to host the two websites on the same instance. I would need to get the database name from the url which is used to navigate the website.

How can I get the url with an express server and pass it to my config variable ?

EDIT : Let's say for example that I have config.websiteDB = defaultDB If a user is visiting the website from website.de, I would like to have config.websiteDB = websiteDEDB and if she is visiting the website from website.en, config.websiteDB = websiteENDB

Quentin Del
  • 1,485
  • 2
  • 18
  • 32
  • 1
    Need more info: are both websites the same codebase? Are you using the npm config module? Anything else that might be relevant? – Jim B. Jan 04 '18 at 18:59
  • Thanks Jim! Both websites have the same codebase, I am not using the npm config module. More details added to the main post. – Quentin Del Jan 04 '18 at 19:15
  • So why can't you just put the mongodb url in the config? – Jim B. Jan 04 '18 at 20:12
  • I would like to change the mongodb url based on the url the visitor of the website has used. (.com or .de) – Quentin Del Jan 10 '18 at 14:52
  • Mongoose isn't really set up for this, but you might find an answer here: https://stackoverflow.com/questions/19474712/mongoose-and-multiple-database-in-single-node-js-project – Jim B. Jan 10 '18 at 18:58

1 Answers1

1

Here is how we solved it.

We used,

https://github.com/mashpie/i18n-node

Also, you can use the same html template files and just have a different content for different languages.

Based on the language received from browser, you can choose appropriate database.

Hope it helps.

EDIT1:

If you check below, you can set your locales based on cookies, querystring parameter, fallback when a language content is not available etc.,

i18n.configure({
    // setup some locales - other locales default to en silently
    locales:['en', 'de'],

    // fall back from Dutch to German
    fallbacks:{'nl': 'de'},

    // you may alter a site wide default locale
    defaultLocale: 'de',

    // sets a custom cookie name to parse locale settings from - defaults to NULL
    cookie: 'yourcookiename',

    // query parameter to switch locale (ie. /home?lang=ch) - defaults to NULL
    queryParameter: 'lang',

    // where to store json files - defaults to './locales' relative to modules directory
    directory: './mylocales',

To set a cookie to desired language,

res.cookie('yourcookiename', 'de', { maxAge: 900000, httpOnly: true });

You can also use querystring parameters

/home?lang=ch

where lang is defined as a locale config parameter.

Kannaiyan
  • 12,554
  • 3
  • 44
  • 83
  • Thanks Kannaiyan. This wouldn't work in my case because I want someone to be able to go on any websites and see the content of its specific url. With your solution a german can only see the german content but what if he wants to see the US one ? – Quentin Del Jan 04 '18 at 19:30
  • It covers whatever the way you want to configure multi language website under express. Will update the answer. – Kannaiyan Jan 04 '18 at 19:34