javascript - how to use PostgreSQL mod for vertx in js? -
hi im new in vertx , want utilize https://github.com/vert-x/mod-mysql-postgresql service utilize code web server
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 json {name:foo, age:13} want insert in 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); }); }
and event click button
$.ajax({ data: {name:foo, age:13} , url: '/foo', type: 'post', datatype: 'json', complete: function (response) { alert(json.stringify(response)); } });
i found how it:
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 json {name:foo, age:13} var jsona={ "action" : "raw", "command" : "select * test" } eventbus.send("postgresql-asyncdb",jsona, function(reply) { req.response.putheader("content-type", "application/json"); req.response.end(json.stringify(reply)); }); }); }
and return:
{"message":"select 6","rows":6,"fields":["testt"],"results":[["lol"],["lolŕ"],["lol2"],["lol2"],["testlol"],["testlolp"]],"status":"ok"}
javascript jquery ajax postgresql vertx.io
No comments:
Post a Comment