0

I have stored an image in directory and saved the path in a mysql table. I am trying to display the image on my ejs page. But it is not displayed. Everything is setup on cloud9. Please tell me what am I doing wrong.

Express file:

var express=require("express");
var app=express();
var mysql=require("mysql");
app.use(express("/home/ubuntu/workspace/Practice"));
var con=mysql.createConnection({
    host:"127.0.0.1",
    user:"akarmalkar",
    password:"",
    database:"c9"
});
con.connect(function(err){
   if(err) throw err;
   console.log("Connection Established");
   /*var sql="select poster from tbl_movies";*/
   var sql="select poster from tbl_movies";
   con.query(sql,function(err,result){
      if(err) throw err;
      console.log(result[0].poster);
      app.get("/:thing",function(req,res){
      res.render("tempEjs.ejs",{resultVar: result[0].poster}); 
      }); 
   });
});

app.listen(process.env.PORT,process.env.IP,function(){
   console.log("Server Started"); 
});

EJS file:

<img src="<%=resultVar%>" width="200px" height="100">

Screenshot of mysql table and folder structure

Apoorv
  • 45
  • 6
  • what does this line log to console? `console.log(result[0].poster);` – defectivepixel Nov 10 '18 at 16:15
  • It prints the value of 'poster' column of the first row, i.e. logan.jpg in this case. – Apoorv Nov 10 '18 at 16:19
  • where do you save your images? this is probably a route related problem. i need to know where exactly you saved you images and what is saved in your database. – defectivepixel Nov 10 '18 at 16:22
  • If you are trying to avoid an extra HTTP call, then you can probably `base64` encode the image and then use that in your template. Check this [answer](https://stackoverflow.com/a/24526156/4924189) on how to do that. – Pramodh Valavala Nov 11 '18 at 02:55
  • I've done it. The path for static folder of images was incorrect. Now I've changed it and everything seems to work fine. – Apoorv Nov 11 '18 at 07:29

0 Answers0