3

I am working on a little fun project(webcalender) and I want to use mongoDB. MongoDB is running and I figured out how to deal with it. I also got the connection to PHP. I was wondering is there any chance to connect to the MongoDB using simple javascript?

I have searched a lot and I always passed by Node.js? Do I need Node.js to connect to mongoDB over Javascript?

Does anyone have a great link? Tutorial? or arguments why I should not do that?

Thanks for help

2 Answers 2

4

there are client side ways of doing this but its not safe at all. there are a few reasons for the lack of security.
1. connection info is in the source for anyone to see.
2. if you use a service like mongoHQ where its a restful API to connect to Mongo your secret is exposed on the client side.
Both of these reasons scared me enough to not use a JS library that allowed me to connect to mongo on client side.

is your application being built in node? or PHP?

if PHP I know theres a PEAR library for MongoDB, then you can use javascript on the client side to interact with php to do what you need on the DB.

if the application is being built in node.js then sure why not? I've had success using Mongoose with express in node.

hope that helps.

Sign up to request clarification or add additional context in comments.

4 Comments

So far the project is built in PHP and MongoDB. Only because of interest I'd like to try out smthg new! So should I use Mongoose and express as frameworks and built it in node.js?
If you would like to learn node as well, I had the same idea, lets learn a noSQL db and better my skills with JS.. Well it was a good week before I was comfortable with node to start messing with MongoDB in it. I would suggest going with the server side language your comfortable with while learning MongoDB.
Okay that sounds good!...last question what exactly is mongoose and express?n and is node.js working if javascript is turned off by the client?
Mongoose and Express are modules for Node, expressjs.com - Express mongoosejs.com - Mongoose JS
1

Yes, you need Node.js to access MongoDB via JavaScript, because simple plain JavaScript runs on the user browser, not on the server, and Node.js is meant to run on the server.

Accessing a database directly from the browser would be a huge security issue, since JS files are always available to those viewing the page.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.