Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect mongoDB to angular2 app?

I have angular2 & mongoDB configured. So far I am able to read json files using http service but I want to develop a complete application which will have database connectivity too.

So i would like to know how can I configure my angular2 app so that it can interact with the database.

Any inputs?

Thanks in advance.

like image 392
Bhushan Gadekar Avatar asked Jul 01 '16 05:07

Bhushan Gadekar


People also ask

How do I connect to a MongoDB database?

Connect to a Single MongoDB Instance const MongoClient = require('mongodb'). MongoClient; const assert = require('assert'); // Connection URL const url = 'mongodb://localhost:27017'; // Database Name const dbName = 'myproject'; // Use connect method to connect to the server MongoClient.

How Android app connect to MongoDB?

Through this, we can connect our android device application to our MongoDB database. So to create a connection method click on Connect button as shown in the image given below. Now after clicking on Connect button the new page will pop up with the option Connect your Application.


1 Answers

You should utilise a framework such as ExpressJS to handle the interaction with the database which returns JSON data in the same way as you are currently doing with a static file.

There are quite a few tutorials around which explain how to do this such as http://adrianmejia.com/blog/2014/10/01/creating-a-restful-api-tutorial-with-nodejs-and-mongodb/

The other option you have is to use a framework to do much of the heavy lifting around building the API. Loopback is a good start for such a task as they have a generator which can generate client side code to interact with your API making the implementation much easier than building everything yourself. I haven't used it in a while so I'm not sure if they have ng2 as a client side generator yet, but it might be a good place to start.

like image 130
Ian Belcher Avatar answered Oct 16 '22 15:10

Ian Belcher