0

我的快递代码::

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

注意::我检查了指定位置有图片,

我在哪里编码错了

4

1 回答 1

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'));
});
于 2013-09-17T12:25:31.033 回答