我有快速代码 ::
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和服务器
snapshot
/public/images
所示的图像已经很少了
现在,如果我在浏览器中测试其中一张图片
http://54.218.73.244:7004/c92beeaf5ba50e65.jpg
我收到如下错误,图片未显示在浏览器中
Cannot GET /c92beeaf5ba50e65.jpg
答案 0 :(得分:0)
<强> Hoping this might help someone
强>
我刚刚将代码行改为 ::
app.use(express.static(__dirname+'/public/images'));