In my application i have to store data to mongodb using nodejs(npm mongodb). I have installed mongodb , nodejs and npm mongodb but the problem here is I installed them in separate folders. I am unable to organize the directory structure properly that's way I am getting errors.If you tell me the folder structure it will help me a lot.
Thanks in advance
Try mongoose - npm install mongoose, mongoose is a ODM for mongodb.
API Documentation - http://mongoosejs.com/
Short example from official documentation:
var mongoose = require('mongoose');
mongoose.connect('localhost', 'test');
var schema = mongoose.Schema({ name: 'string' });
var Cat = mongoose.model('Cat', schema);
var kitty = new Cat({ name: 'Zildjian' });
kitty.save(function (err) {
if (err) // ...
console.log('meow');
});
Run npm install mongodb command in your application directory. For example, nodejsapp folder is the application directory.
The folder structure inside the directory will look like :
nodejsapp
├─────────── node_modules <-- All the node modules installed using npm comes here -->
| ├──────────── .bin
| ├──────────── express
| └──────────── mongodb
├─────────── app <-- holds all our files for node components (models, routes)-->
├─────────── config <-- all our configuration will be here -->
├─────────── public <-- holds all our files for our front-end application -->
├─────────── server.js <-- Node configuration -->
The node_modules folder is created when you use npm command in the application directory.
This is a better way to organize the node packages for a project.