2

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:password@example.com/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!

  • Not so fast! Read about [data model](http://www.webdeveasy.com/angularjs-data-model/) first. And the answer it "from a server". – Raz Feb 21 '17 at 14:50
  • I'm not sure if I understand what you mean @Raz. Would you elaborate that? I am already a little bit familiar with the data model, but how does that respons to where in the code I place the database connection? – InfinityStream Feb 21 '17 at 17:26

1 Answers1

3

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.

Raz
  • 7,508
  • 4
  • 27
  • 25