Display image using EJS in node.js / express

I'm just trying to configure using node.js / express / ejs. I know that ejs is not actual HTML, and therefore it is difficult for me to find a simple image. Can someone point me in the right direction?

Directory structure:

  • MYAPP / server.js
  • MYAPP / views / index.ejs
  • MYAPP / logo.jpg

I have now

// index.ejs <img src = "../logo.jpg" /> 

Am I really wrong? Thanks.

+6
source share
2 answers

Static files in Express must be inside the directory specified in your static middleware. This is usually ./public/ .

For example, in your server.js you might have something like this:

 app.use( express.static( "public" ) ); 

Each file inside this folder will be accessible from the root URL, so this will work:

 <img src="logo.jpg" /> 
+13
source

You need to assign app.use( express.static( "public" ) ); on app.js and then don’t forget / as root:

 <img src="/images/logo.jpg" /> 

the folder with images should be in the shared folder:

 - public/ - images/ - logo.png - app.js 
0
source

Source: https://habr.com/ru/post/949864/


All Articles