3

I'm facing a problem with paths in nodeJs, I route the user to the index page when he specifies the language in the url like this :

   app.all('/:locale?/index',function(req,res,next){
      if(!req.params.locale){
         console.log('no param');
     res.render('index');
      } else {
       var language = req.params.locale;
        i18n.setLocale(language);
       res.render('index');
      }
    });

However, in my index.html page the source of images are specified this way : ./images/img1.png , when I route the user, my index.html shows image not found because it considers the path " lang/images/img1.png , it considers the languge in my URL, could you please help?

Thank you

1 Answers1

2

The . in your path is telling the app to look at the current folder, which is lang. You should be able to get around this by specifying either a URL:

<img src="http://myApp.com/images/img1.png">

or by specifying the path from the root directory (everything except http://myApp.com)

<img src="/images/img1.png">

This is probably a better solution, since you can swap your domain easily; for example, working on your local machine (http://localhost:3000/) vs. a deployed app (http://myApp.com)

Generally speaking, I'd almost always use the path from root rather than a relative path (e.g., ./...), since I may move pages around in refactoring, and it's easier to look for direct references than relative ones if I have to change anything.

Carson Moore
  • 1,287
  • 1
  • 8
  • 9
  • Thank you for your answer, I'll try this and keep you informed about he resutl. Btw, do you know the module i18n ? I only wanna confirm if the way I'm developing is the right / best one – yatika mika Apr 27 '15 at 12:22
  • @yatikamika I've used i18n, but I'm certainly not an expert. I'm sure you can google around for best practices with it, though (e.g., a quick search found http://ejohn.org/blog/a-strategy-for-i18n-and-node/, which seems like it might be a good start). It recommends sub-domains (lang.myApp.com), but I'd check other sources too. – Carson Moore Apr 27 '15 at 12:26
  • I've checked, but seriosly I didn't find answers to my questions, I will use the subdirectories, but i don't know if it's better to put the language here www.example.com/fr-FR/index.html or www.example.com/index.html/fr-FR. I don't know if I have to redirect, or only use a render to keep fr-FR in the URL , do you have an idea about those type of questions? – yatika mika Apr 27 '15 at 12:30
  • That's a bit beyond my expertise, unfortunately. Maybe another question is warranted? – Carson Moore Apr 27 '15 at 12:43
  • Yes :) , when we detect the variable before index '/:locale?/index' , How can I check if it exists in supported languages? because actually when I enter www.example.com/somethingAnything/index.html, it redirects me :/ it consider SomethingAanything as a language and it doesn't automatically check it in the Supported languages, I'm I obluge to make a loop in order to check that.? – yatika mika Apr 27 '15 at 13:18
  • it does not work :( with /images/img1.png it took also the /fr-FR/ , any other suggestion? – yatika mika Apr 27 '15 at 18:14
  • @yatikamika Do you have any catch-all routing that would do this (e.g., `app.all('*', function(req, res, next) {...} )`? – Carson Moore Apr 27 '15 at 19:29
  • No,I only handle when the user puts /index , /about , .contact, could you please show me how I should handle this? – yatika mika Apr 27 '15 at 20:12