通过JavaScript进行PostgreSQL连接 [英] PostgreSQL connection via javascript

查看:1262
本文介绍了通过JavaScript进行PostgreSQL连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种直接在浏览器中连接到postgresql的方法。我试图利用nodejs和browserify,但到目前为止还没有运气。每当我编译包含

I am searching for a way to connect to postgresql directly in the browser. Im trying to utilize nodejs and browserify but have had no luck so far with the bundling. Whenever I compile a script that contains a

require('pg')

它在浏览器中特别声明:

it specifically states in the browser:


找不到模块' / node_modules / pg / lib / client'

Cannot find module '/node_modules/pg/lib/client'

浏览器此后告诉我,他无法找到pg所需的模块。也许我之前需要将pg与browserify捆绑在一起?

the browser tells me afterwards that he is not able to find the modules that pg requires. Maybe i need to bundle pg with browserify before ?

如果有人对如何解决这个问题有一个想法,甚至有人建议我可以通过javascript连接到pgsql,我都表示感谢。

I appreciate if somebody has an idea on how to work on this or even a suggestion how I could connect via javascript to pgsql.

推荐答案

虽然某些节点模块通常可以在某种程度上在Web浏览器中重用,但是大多数都使用Node.JS特定的功能或驱动程序,并且不能在任何Web浏览器中使用。像Postgresql软件包这样的情况就是一个很好的例子,因为它需要Web浏览器中根本不存在的许多功能。

While some node modules may be generally reusable to some degree in a web browser, most take advantage of Node.JS specific features or drivers and cannot work in any web browser. A case like a Postgresql package is a perfect example, as it requires many functions simply not present in a web browser.

如果您查看Node.JS的API( http: //nodejs.org/api/ ),这些API在浏览器中不可用(某些API可以被仿真,但是许多是文件系统,低级套接字,二进制模块等)。

If you look at the APIs of Node.JS (http://nodejs.org/api/), these APIs are not available in the browser (some could be emulated, but many are file system, low level sockets, binary modules, etc.).

如果您想使用Postgresql,则需要构建一个Web服务器层,并公开自己的API(可能是RESTful风格的api)并调用Web服务来执行您要使用的数据库操作。您可以使用连接 Express 使编写Web服务层更加方便。

If you want to use Postgresql, you'll need to build a web server layer, and expose an API of your own (likely a RESTful style api) and call the web services to perform the database actions you want to use. You could look at using Connect or Express to make writing the web service layer more convenient.

这篇关于通过JavaScript进行PostgreSQL连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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