Paths in nodejs

I have a problem with paths in nodeJs, I route the user to the index page when he sets 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 image source is specified like this: ./ images / img1.png, when I route the user, my index.html shows the image not found because it considers the path "lang / images / img1.png, it thinks it is my url, can you please help?

thank

+3


source to share


1 answer


.

in your path tells the application to look at the current folder that is in lang

. You can get around this by either specifying 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 the best solution since you can easily change your domain; for example working on your local machine ( http://localhost:3000/

) versus deployed application ( http://myApp.com

)

Generally speaking, I've almost always used a path from the root rather than a relative path (for example ./...

), since I can move pages in refactoring, and it's easier to find direct links than relative ones if I have to change something.

+2


source







All Articles