How to connect to MongoDB using PhantomJS

2019-04-09 18:52发布

问题:

I'm Running OSX. Thought I could find documentation somewhere, but could not.

回答1:

You can use a MongoDB REST client:

  • Sleepy.Mangoose: It's the best IMO

  • MongoDB REST: It looks promising, but is not complete yet and has some weird bugs (for example, it refused to send large requests (more than 80,000 characters) and it took me 3 hours to find what was wrong with it)

  • MongoDB Java REST server

You can connect directly to the rest server from you injected javascript (use jQuery's ajax method or simply use xmlhttprequest), but I think it's better to do it inside PhantomJS's evaluate method (because if the page you're parsing has javascript errors, your the injected javascript might not work properly).


But I personally don't like the idea of a REST server for PhantomJS. I think It's better to write your whole application in NodeJS and use phantomjs module to run PhantomJS. This way, you don't have to go the REST way and can use the (now official) MongoDB Native Node Driver, which is potentially many times faster than the REST servers (because it doesn't require JSON HTTP serialization/deserialization, among other things).