我可以在客户端(express / node.js)中使用PostgreSQL(pg) [英] Can I use PostgreSQL (pg) in the client-side (express/node.js)

查看:377
本文介绍了我可以在客户端(express / node.js)中使用PostgreSQL(pg)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在客户端使用PostgreSQL。那可能吗?我可以使用这个代码吗?

I want to use PostgreSQL in the client side. is that possible? can i have this code?

在我的server.js中

in my server.js

   var pg = require('pg');

在我的客户端

     var conString = "postgres://postgres:pass@localhost/mydb";
     var client = new pg.Client(conString);
     client.connect();
     var query = client.query("SELECT * FROM users ");        
     query.on('row', function(row) {
             alert(row.name);
     });

我尝试过这个代码,但没有发生任何事情。虽然我可以使用类似的代码,我连接PostgreSQL并在客户端脚本中使用查询。

i tried this code but nothing happened. though can i have a code similar to this, where i connect PostgreSQL and use queries on the client scripts.

推荐答案

不,这是(显然)不可能。你不想让客户端直接访问你的数据库。除此之外,即使您在客户端和服务器端都使用JS,与使用例如PHP或Python在服务器上 - 它可以通过AJAX和常规的http请求进行唯一的沟通。

No, that's (obviously) not possible. You wouldn't want to let a client access your database directly anyway. Besides that, even though you use JS on both the client and server side it's not different from what happens if you use e.g. PHP or Python on the server - the only communication between it is possible via AJAX and regular http requests.

这篇关于我可以在客户端(express / node.js)中使用PostgreSQL(pg)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆