Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying an image with EJS in node.js/express

Tags:

I'm just trying to get setup with node.js/express/ejs. I know ejs isn't actual HTML and so I'm having a hard time just displaying a simple image. Can someone point me in the right direction?

Directory structure is:

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

Right now I have

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

Am I going about this the wrong way? Thanks.

like image 654
gone Avatar asked Jul 19 '13 20:07

gone


People also ask

Is EJS and Express js same?

js meets the needs of their business better than EJS. When comparing quality of ongoing product support, reviewers felt that Express. js is the preferred option. For feature updates and roadmaps, our reviewers preferred the direction of Express.


1 Answers

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

For example, in your server.js you may 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" /> 
like image 55
gustavohenke Avatar answered Sep 20 '22 21:09

gustavohenke