2012-10-15 27 views
-1

所以我要下一个测试代码,我发现heremysql的模块没有我的情况下工作

// Include http module, 
var http = require('http'), 
// And mysql module you've just installed. 
    mysql = require("mysql"); 
// Create the connection. 
// Data is default to new mysql installation and should be changed according to your configuration. 
var connection = mysql.createConnection({ 
    user: "root", 
    password: "pass", 
    database: "db_name" 
}); 
// Create the http server. 
http.createServer(function (request, response) { 
    // Attach listener on end event. 
    request.on('end', function() { 
     // Query the database. 
     connection.query('SELECT * FROM your_table;', function (error, rows, fields) { 
     response.writeHead(200, { 
      'Content-Type': 'x-application/json' 
     }); 
     // Send data as JSON string. 
     // Rows variable holds the result of the query. 
     response.end(JSON.stringify(rows)); 
     }); 
    }); 
// Listen on the 8080 port. 
}).listen(8080); 

我下载mysql的模块,安装当然MySQL和运行下一个脚本,并获得无结果。你能给我建议我做错了什么吗?

当我试图加载

http://localhost:8080/ 

浏览器尝试加载网页几分钟,还没有结果。

谢谢。

更新 enter image description here

+0

[Node.js的从http请求响应不调用 '结束' 事件的可能的复制,而不包括 '数据'事件](http://stackoverflow.com/questions/23817180/node-js-response-from-http-request-not-calling-end-event-without-including-da) –

回答

2

我想你忘记了

connection.connect() 

我使用这些的,而现在,它为我工作得很好。

// Include http module, 
var http = require('http'), 
// And mysql module you've just installed. 
    mysql = require("mysql"); 
// Create the connection. 
// Data is default to new mysql installation and should be changed according to your configuration. 
var connection = mysql.createConnection({ 
    host: "localhost", 
    user: "root", 
    password: "pass", 
    database: "db_name" 
}); 
connection.connect(); 
// Create the http server. 
http.createServer(function (request, response) { 
    // Attach listener on end event. 
    request.on('end', function() { 
     // Query the database. 
     connection.query('SELECT * FROM your_table;', function (error, rows, fields) { 
     response.writeHead(200, { 
      'Content-Type': 'x-application/json' 
     }); 
     // Send data as JSON string. 
     // Rows variable holds the result of the query. 
     response.end(JSON.stringify(rows)); 
     }); 
    }); 
// Listen on the 8080 port. 
}).listen(8080); 
+0

我现在检查它 –

+0

hm ,我查nge我的代码根据你的回答,但结果相同 –

+0

npm install [email protected]是否足以安装mysql模块? –

0

很少有以前的答案,应该帮助你

Express.JS + Node-Mysql and 1 connection per http request

How to query mysql before starting http server in nodejs

get simple info from db into a node.js http server response

在MySQL使用池 Node.js MySQL Needing Persistent Connection

更复杂的解决方案将您的代码分隔成多个文件,并将db代码放入db.js或reponse处理程序或路由器中。

我的代码正在使用池。

保存到五档 然后再运行安装NPM

e.g (请原谅可怕的格式)

// requestHandlers.js 
    // request handler with mysql code 

    var mysql = require("mysql"); 
    var Query1 = "SELECT * from Tablexyz WHERE xx = 'doh'"; 

    var body = '<html>'+ 
'<head>'+ 
'<meta http-equiv="Content-Type" content="text/html; '+ 
'charset=UTF-8" />'+ 
'</head>'+ 
'<body>'+ 
'<h1> Query </h1>'; 

    var body1 = '</body>'+ 
'</html>'; 

    var pool = mysql.createPool({ 
    host  : 'host', 
    user  : 'dbuser', 
    password : "pword", 
    database : 'database', 
    connectionLimit: 10, 
    queueLimit: 10 
    }); 


    function qquery(callback){ 
    console.log('in qquery'); 
     pool.getConnection(function(err, connection) { 
     pool.query(Query1 , function(err, rows ,fields) { 
      if (err) { 
        return callback(new Error("An error has occured" + err)); 
       } 
      if (rows.length > 0){ 
      callback(rows); 
      connection.release();   
      } 
     }); 
     }); 
     } 

    function start(response) { 
    console.log("Request handler 'start' was called."); 


    qquery(function (rows , err) { 
      if (err) { 
       console.log(err); 
      } 
      else{ 
       console.log(rows); 
       response.writeHead(200, {"Content-Type": "text/html"}); 
          response.write(body); 
       response.write('query: ' + JSON.stringify(rows)); 
       response.write(body1); 
          response.end(); 
      } 
    }); 


} 

exports.start =启动;

///////////////////////////////////- new file 
    //server.js 

    var http = require("http"); 
    var url = require("url"); 

    function start(route, handle) { 
    function onRequest(request, response) { 
    var pathname = url.parse(request.url).pathname; 
    console.log("Request for " + pathname + " received."); 
    route(handle, pathname, response); 
    } 
    http.createServer(onRequest).listen(8888); 
    console.log("Server has started."); 
    } 

    exports.start = start; 
    ///////////////////////////////////-new file 
    // router.js 

    function route(handle, pathname, response) { 
    console.log("About to route a request for " + pathname); 
    if (typeof handle[pathname] === 'function') { 
    handle[pathname](response); 
    } else { 
    console.log("No request handler found for " + pathname); 
    response.writeHead(404, {"Content-Type": "text/plain"}); 
    response.write("404 Not found"); 
    response.end(); 
    } 
    } 

    exports.route = route; 
    ///////////////////////////////////- new file 
    //index.js 

    var server = require("./server"); 
    var router = require("./router"); 
    var requestHandlers = require("./requestHandlers"); 
    var handle = {} 
    handle["/"] = requestHandlers.start; 
    handle["/start"] = requestHandlers.start; 
    handle["/upload"] = requestHandlers.upload; 
    server.start(router.route, handle); 

    ///////////////////////////////////- new file 
    //package.json 
    { 
    "name": "application-name", 
    "version": "0.0.1", 
    "private": true, 
    "scripts": { 
    "start": "node app.js" 
    }, 
    "dependencies": 
    { 
     "express": "3.4.0", 
     "jade": "*", 
     "stylus": "*", 
     "mysql": "*" 
    } 

}

0

@Ishikawa耀

使用回调

// Include http module 
var http = require('http') 
// And mysql module you've just installed. 
mysql = require("mysql"); 

var Query1 = "SELECT * from Tablexyz WHERE xx = 'doh'"; 

    var connection = mysql.createConnection({ 
    host  : 'host', 
    user  : 'dbuser', 
    password : "pword", 
    database : 'database', 
    }); 

    connection.connect(); 

    function getData(callback){ 
    console.log('in getData'); 
    connection.query(Query1 , function(err, rows ,fields) { 
    if (err) { 
     return callback(new Error("An error has occured" + err)); 
    } 
    if (rows.length > 0){ 
     callback(rows); 
    } 

}); 
} 

// Create the http server. 
http.createServer(function (request, response) { 
// Attach listener on end event. 

    // Query the database. 
getData(function (rows , err) { 
    if (err) { 
     console.log(err); 
    } 
    else{ 
     response.writeHead(200, {"Content-Type": "text/html"}); 
     response.write('query: ' + JSON.stringify(rows)); 
     response.end(); 
    } 
}); 
    // Listen on the 4001 port. 
    }).listen(4001); 
+0

仍然无法获得格式化权限doh – lxx

相关问题