我的快递代码::
ar express = require('express')
, http = require('http')
, mysql = require('mysql'); // <---- HERE
var app = express();
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: "root",
database: 'test123'
});
connection.connect(); // <---- AND HERE
// all environments
app.set('port', process.env.PORT || 7005);
app.get('/',function(request,response){
connection.query('SELECT * FROM test456', function(err, rows, fields)
{
console.log('Connection result error '+err);
console.log('no of records is '+rows.length);
response.writeHead(200, { 'Content-Type': 'application/json'});
response.end(JSON.stringify(rows));
});
} );
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
我使用了以下SQL查询来创建一个有害的数据库
CREATE TABLE test456 ( _id INT(10), name VARCHAR(30), image VARCHAR(50));
INSERT INTO test456 ( _id,name,image ) VALUES ( 1,"nature","http://54.218.73.244:7005/image.jpg" );
然后
mysql> SELECT * FROM test456;
+------+--------+-------------------------------------+
| _id | name | image |
+------+--------+-------------------------------------+
| 1 | nature | http://54.218.73.244:7005/image.jpg |
+------+--------+-------------------------------------+
1 row in set (0.00 sec)
但是当我通过浏览器尝试网址时,我无法获取图片
http://54.218.73.244:7005/image.jpg
我输出为::
Cannot GET /image.jpg
注意::我已检查指定位置是否有图像
我在编码时出错
答案 0 :(得分:0)
在聊天中讨论之后,试试这个:
var express = require('express')
, http = require('http')
, mysql = require('mysql'); // <---- HERE
var app = express();
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: "root",
database: 'test123'
});
connection.connect(); // <---- AND HERE
// all environments
app.set('port', process.env.PORT || 7005);
// this will catch
app.get('/countries/:country_id',function(req, res) {
// you don't know what was passed, so escape it.
connection.query('SELECT * FROM test456 where country_id = ?', [mysql.escape(req.params.country_id)], function(err, rows, fields) {
console.log('Connection result error '+err);
console.log('no of records is '+rows.length);
response.writeHead(200, { 'Content-Type': 'application/json'});
response.end(JSON.stringify(rows));
});
} );
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});