无法在浏览器中显示简单快速代码的图像

时间:2013-12-17 10:08:32

标签: node.js express node-mysql

我有快速代码 ::

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'));

});

快照显示我已连接到mysql和服务器 enter image description here

我的位置snapshot

中的/public/images所示的图像已经很少了

enter image description here


现在,如果我在浏览器中测试其中一张图片

http://54.218.73.244:7004/c92beeaf5ba50e65.jpg

我收到如下错误,图片未显示在浏览器中

Cannot GET /c92beeaf5ba50e65.jpg

如何解决这个问题! ! !

1 个答案:

答案 0 :(得分:0)

<强> Hoping this might help someone

在其中一个答案的Andrew的帮助下,我解决了这个问题

我刚刚将代码行改为 ::

app.use(express.static(__dirname+'/public/images'));