Pergunta

I have my express code::

var express=require('express');
var fs=require('fs');
var http=require('http');
var crypto=require('crypto');
var mysql=require('mysql');
var async=require('async');

var app=express();

var connection=mysql.createConnection({
        host: 'localhost',
        user: '************',
        password: '************',
        database: 'ImagePostingDB'
});

connection.connect(function(err) {
        if ( !err ) {
                     console.log("Connected to MySQL");
                    } else if ( err )
                    {
                         console.log(err);
                    } });


app.set('port',process.env.PORT||7004);
app.use('/Details',express.static(__dirname+'/public/images'));
app.use(express.bodyParser());



app.get('/DescriptionSortedPrice/',function(request,response){
  connection.query('SELECT * FROM ImagePostingtable ORDER BY Sl_no', function(err, rows, fields) {
    if (err) {
      return response.send(500, err.message);
    }
        console.log('Found results:', rows);
    response.json({
      'restaurants' : rows
    });
  });
});

app.post('/Details/',function(req,res,next) {
  var file_name=req.files.key.originalFilename;
  var file_name1=req.body.key1;

  var name;

  console.log(file_name);
  console.log(file_name1);


  async.series( [
    // Get the first table contents
    function ( callback ) {
      crypto.randomBytes(8, function(ex, buf) {

        var array = req.files.key.originalFilename.split('.');
        var type  = array[array.length - 1];
        name  = buf.toString('hex') + '.' + type;

        fs.rename(req.files.key.path, './public/images/' + name, function(e) {

          if (e) {
            res.send(500, e.message);
          } else {
            res.send("I got the message - This i confirm");
          }

          return callback(null);
        });
      });
    },

    // Updating the database
    function ( callback ) {
      connection.query('INSERT INTO ImagePostingtable (Image_Name,Person_Name) VALUES (?,?)', [name,file_name1], function (err, rows, fields) {
        console.log('Connection result error ' + err);
        return callback(null);
      });
    }
  ]);
});

app.get('/Details/',function(req,res){
        res.send("Image displayed");
});

http.createServer(app).listen(app.get('port'),function(){
        console.log('Express server listening on port'+app.get('port'));

});

Snapshot shows i am connected to mysql and to server enter image description here

I already have few images as shown in the snapshot below in the location /public/images

enter image description here


Now if i test in my browser for one of the images

http://54.218.73.244:7004/c92beeaf5ba50e65.jpg

i get error as below and image is not displayed in browser

Cannot GET /c92beeaf5ba50e65.jpg

HOW TO RESOLVE THIS ! ! !

Foi útil?

Solução

Hoping this might help someone

With the help of Andrew in one of the answers i resolved this

I just changed the line of code to below::

app.use(express.static(__dirname+'/public/images'));
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top