NodeJS 33: MongoDB connect using Mongoose
https://scotch.io/tutorials/using-mongoosejs-in-node-js-and-mongodb-applications
https://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html
In Terminal:
npm install mongoose --save
items.js (Create Model)
// grab the things we need
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// create a schema
var itemSchema = new Schema({
item: String,
available: Boolean,
soldQty: Number
});
// the schema is useless so far
// we need to create a model using it
var Item = mongoose.model('Item', itemSchema);
// make this available to our users in our Node applications
module.exports = Item;
IPLService.js (Save Model to MonogoDB)
var express = require('express');
var Item = require('./item');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
app.post('/BuildFlows',function(req,resp){
resp.writeHead(200,{'Content-Type':'application/json','Access-Control-Allow-Origin' : '*','Access-Control-Allow-Methods': 'GET,POST,OPTIONS,DELETE,PUT'});
console.log('Inside post - BuildFlows');
console.log(req.body);
var item1 = new Item(req.body);
console.log("item1 : "+item1);
item1.save(function(err) {
if (err) throw err;
console.log('Item saved successfully...........');
});
resp.setHeader('Content-Type', 'application/json');
resp.end(JSON.stringify(req.body, null, 3));
});
