Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
That seems to be the only option but exactly what i tried to circumvent by the browserify approach. thanks anyway –  ins0m Oct 27 '13 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 Oct 27 '13 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 Oct 27 '13 at 17:47
    
Many don't work, especially those that do IO. –  WiredPrairie Oct 27 '13 at 19:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.