Thiago Loddi
Thiago Loddi

Reputation: 2340

image in .ejs not showing when using WebStorm

I'm using Webstorm to create a page and I can't get images to show when rendering .ejs. Here is my code:

test.js

http = require('http')
express = require('express')
app = express()
app.engine('.ejs', require('ejs').__express)

var port = process.env.PORT ? process.env.PORT : 3000
http.createServer(app).listen(port, null, null, function(){
    console.log('Listening to port ' + port)
})

app.get('/test', function(req, res){
    res.render('test.ejs')
})

test.ejs

Note: When I open this exactly same code using Google Chrome the image renders perfectly. When I try using http://localhost:3000/test I get a generic image icon.

<div>
  <p style="font-size: 40px; font-style: oblique;">My Page</p>
  <form action="/next" method="post">
    <img src="../images/test.png" >
    <p>Preço: R$ 100,00</p>
    <input name="next" id="next" type="submit" value="next">
  </form>
</div>

Could this be anything to do with permissions? I've changed permission for my /images folder and test.png file using:

sudo chmod 0755 /images
sudo chmod 0644 test.png

but still didn't work. Thanks!

Upvotes: 1

Views: 530

Answers (1)

lena
lena

Reputation: 93728

When using the following project structure:

app.js
public
      --
        images
        test.ejs
                --
                  test.png

I managed to get it working wher using the following code:

http = require('http')
express = require('express')
app = express()
app.engine('.ejs', require('ejs').__express)
app.set('views', __dirname + '/public')
app.set('view engine', 'ejs')
var port = process.env.PORT ? process.env.PORT : 3000
http.createServer(app).listen(port, null, null, function(){
    console.log('Listening to port ' + port)
})

app.get('/test', function(req, res){
    res.render('test.ejs')
})
app.use(express.static(__dirname + '/public'))

.ejs:

<div>
    <p style="font-size: 40px; font-style: oblique;">My Page</p>
    <form action="/next" method="post">
        <img src="images/test.png" >
        <p>Pre?o: R$ 100,00</p>
        <input name="next" id="next" type="submit" value="next">
    </form>
</div>

Upvotes: 1

Related Questions