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:
Cannot find module '/node_modules/pg/lib/client'
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 ?
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.
1 Answer 1
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.
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.).
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.
-
That seems to be the only option but exactly what i tried to circumvent by the browserify approach. thanks anywayins0m– ins0m2013年10月27日 14:30:17 +00:00Commented Oct 27, 2013 at 14:30
-
You're still missing the point of what browserify does. It can't make things compatible with the browser if the APIs the code uses aren't available to the browser.WiredPrairie– WiredPrairie2013年10月27日 14:38:55 +00:00Commented Oct 27, 2013 at 14:38
-
I a not missing that - I'm perfectly clear about what browserify does. But it was not clear to me which apis weren't accessible.ins0m– ins0m2013年10月27日 17:47:36 +00:00Commented Oct 27, 2013 at 17:47
Explore related questions
See similar questions with these tags.