InfinityStream
InfinityStream

Reputation: 45

Connect to Mongodb from Angular, from where?

I have this admin panel template that's built in nodejs, jquery and angular.

I am trying to connect it to a mongodb to make simple CRUD operations.

I've installed mongojs via npm for this purpose but how do I take it from here? The Datebase itself is already set up and ready for use.

I tried to follow the instructions but I am not quite sure where to put the code that connects to the database.

var databaseUrl = "mydb"; // "username:[email protected]/mydb"
var collections = ["users", "reports"]
var db = require("mongojs").connect(databaseUrl, collections);

I've understood that it has to be on the server side as the client side won't run the require('mongojs') part. But in what file should it preferably be placed? And if it's put in the server side code how do I reach the 'db' object from the client side when making the CRUD operations?

Thanks in advance!

Upvotes: 2

Views: 2880

Answers (1)

Raz
Raz

Reputation: 7923

The server and the clients are different devices that interact by HTTP. Consider them as different projects that can luckily execute same chunks of code just because they are written in the same language. DB connection is not this kind of chunk.

Client doesn't connect to the database. You can't give db access to all your clients. Actually db should not be accessible from the Internet at all for security reasons.

Client makes HTTP requests to the server. Server fetches the db data and returns it back to the client. It is the main purpose of almost all servers.

This data updates the state of the models in your controller code.

Upvotes: 3

Related Questions