ktlim
ktlim

Reputation: 249

Can angularjs connect directly to mongodb?

I am new to angularjs and as the title said, I am wondering if there is a way to connect angularjs directly to mongodb without coding additional server side using express.js. i tried to search on the Internet but i cannot find any resources.

Upvotes: 5

Views: 3014

Answers (2)

Eman Jayme
Eman Jayme

Reputation: 240

Angular can use MongoJS to connect to MongoDB.

Installation of MEAN

>Npm init
>npm install express body-parser ejs mongojs --save

 Todos.js

 var db = mongojs('mongodb://eman:[email protected]:27017/todos',['tasks']);
 db.tasks.find( function(err, tasks)  {
    if (err) {
       res.send(err);
    }else {
       res.json(tasks);
    }
  });

//IP and Port can be seen when from your terminal when you run "MONGO"

Must install MongoDB from localhost.

Upvotes: -1

AlphaG33k
AlphaG33k

Reputation: 1678

Sorry what you are trying to do in not possible. You will need to introduce some serverside technologies so that you can talk to the database and form some sort of api that will return JSON data depending on certain business rules coded into the backend of your application. AngularJS has templating built into it which receives JSON data and places it as you direct it to throughout the DOM.

What your asking is logical, I used to wonder this as well coming from the frontend world. If this no-server side code where ever to happen, the database queries would be exposed to the user on the client side. The client could then modify the AngularJS "query syntax" in the code inspector. BAM... now all of the data held in your database is now exposed to the user. Ever worse this could mean that the user could perform delete ops and whatnot. Anyways I hope this sheds a bit of light on the subject for you! Here two resources that helped me on this server/client stuff:

http://tomdale.net/2015/02/youre-missing-the-point-of-server-side-rendered-javascript-apps/

https://medium.com/google-developers/tradeoffs-in-server-side-and-client-side-rendering-14dad8d4ff8b

Upvotes: 7

Related Questions