2014-06-19 49 views
-2

喜即時通訊新,我想用https://github.com/vert-x/mod-mysql-postgresql的服務 我使用此代碼爲我的Web服務器如何在js中使用PostgreSQL mod for vertx?在vertx

var vertx = require('vertx'); 
    var console = require('vertx/console'); 

    var Server = vertx.createHttpServer(); 

    Server.requestHandler(function (req) { 
     var file = req.path() === '/' ? 'index.html' : req.path(); 
     if (file === '/foo') { 
      foo(req); 
     }  
     else{ 
     req.response.sendFile('html/' + file); 
     } 
    }).listen(8081); 

    function foo(req) {  
     req.bodyHandler(function (data) { 
      //data is json {name:foo, age:13} i want insert this in any table in postgre 
      //do 
      var dataresponse= messagefrompostgre;//e: {status:"ok", code:200, message: "its ok"} 
      req.response.putHeader("Content-Type", "application/json");        
      req.response.end(dataresponse); 
     }); 
    } 

,這是我的情況下點擊按鈕

$.ajax({ 
       data: {name:foo, age:13} , 
       url: '/foo', 
       type: 'post', 
       dataType: 'json', 
       complete: function (response) { 
         alert(JSON.stringify(response));      
       } 
      }); 
+0

此問題缺少一個重要部分:問題描述。 – Sumurai8

回答

0

我發現了怎麼辦它:

var vertx = require('vertx'); 
    var console = require('vertx/console');//TODO: remove 
    var eventBus = vertx.eventBus; 

    var Server = vertx.createHttpServer(); 

    Server.requestHandler(function (req) {  
     var file = req.path() === '/' ? 'index.html' : req.path(); 
     if (file === '/foo') { 
      foo(req); 
     } 
     else{ 
     req.response.sendFile('html/' + file); 
     } 
    }).listen(8081); 

    function foo(req) {  
     req.bodyHandler(function (data) { 
      //data is json {name:foo, age:13}     
      var jsona={ 
         "action" : "raw", 
         "command" : "select * from test" 
        }    

      eventBus.send("PostgreSQL-asyncdb",jsona, function(reply) { 
      req.response.putHeader("Content-Type", "application/json");       
      req.response.end(JSON.stringify(reply));     
      }); 
     }); 
    } 

,這回:

{"message":"SELECT 6","rows":6,"fields":["testt"],"results":[["lol"],["lolŕ"],["lol2"],["lol2"],["testlol"],["testlolp"]],"status":"ok"}