This particular combination of tools has generated a lot of traction in the enterprise area and is framework based, making it a good place to start.
Before you begin we recommend you read about the basic building blocks that assemble a MEAN application:
// Updated By : Om Prakash
// Updated on : 14 March 2016
//Purpose : (1.0) Insert and Get Repository in Mongo DataBase.
var express = require('express');
var fs = require('fs');
var mongoClient = require('./Mongo.js'); // For Modularity
var mkdirp = require('mkdirp');
var app = express();
app.use(express.static('public'));
app.use(express.static('WebContent'));
var appConfig = require('./AppConfig');
//Get Repository fields 1.0
app.get('/repository/getFields', function (req, res) {
console.log("My Request This", req.query);
mongoClient.getRepositoryFields(req.query.repositoryName, function (err, result) {
if (err) {
res.status(500).send("Oops! Error while get Source configuration. Please contact your administrator.")
return;
}
console.log("Result This", result);
res.send({ err: null, data: result});
});
});
//Get Repository Data 1.2
app.get('/repository/getRepositoryData', function (req, res) {
console.log("My Request This", req.query);
mongoClient.getRepositoryData(req.query.repositoryName, function (err, result) {
if (err) {
res.status(500).send("Oops! Error while get Source configuration. Please contact your administrator.")
return;
}
console.log("Result This", result);
res.send({ err: null, data: result });
});
});
//get Repository data 1.0
app.get('/repository/repositoryList', function (req, res) {
console.log('going to get recon results');
mongoClient.getRepository({}, function (err, result) {
console.log("req This", req.params);
if (err) {
res.status(500).send("Oops! Error while get Source configuration. Please contact your administrator.")
return;
}
res.send({ err: null, data: result });
});
});
//save Repository 1.0
app.post('/repository', LoaderInput.any(), function (req, res) {
mongoClient.saveRepository(req.body, function (err, result) {
if (err) {
//res.status(500).send("Oops! Error while Save repository. Please contact your administrator.")
res.status(500).send(err);
return;
}
res.send({ err: err, data: result });
});
});
var port = appConfig.getConfig("ServerPort");
app.listen(port, function () {
console.log("server started on port : " + port);
});
// Created By : Om Prakash
// Created on : 14 March 2016
//Purpose : (1.0) Create for Mongo DataBase Operation.
var appConfig = require('./AppConfig'); // For Modularity
var mongoClient = require('mongodb').MongoClient;
var assert = require('assert')
var ObjectId = require('mongodb').ObjectID;
var mongoURL = 'mongodb://' + appConfig.getConfig("mongoServerIP") + ':' + appConfig.getConfig("mongoServerPort") + '/' + appConfig.getConfig("mongoDBName");
// Insert collection
var insertCollection = function (TableName, doc, callback) {
mongoClient.connect(mongoURL, function (err, mongodb) {
//assert.equal(err, null);
if(err){
console.log("unable to connect to db:", mongoURL);
console.log(err);
callback(err.message,null);
return;
}
//console.log("connected to db:", mongoURL);
mongodb.collection(TableName).insert(doc, function(err, result){
if(err){
console.log(err);
callback(err.message,null);
return;
}
callback(err, result);
mongodb.close();
});
});
};
// Table Exists
var getTableExists = function (TableName, doc, callback) {
//console.log("Collection exists!", TableName);
mongoClient.connect(mongoURL, function (err, mongodb) {
//assert.equal(err, null);
if(err){
console.log("unable to connect to db:", mongoURL);
console.log(err);
callback(err.message,null);
return;
}
mongodb.collection("system.namespaces").find({ name: appConfig.getConfig("mongoDBName") + "." + TableName }).toArray(function (err, result) {
//console.log("Collection doesn't e", result);
if (result.length) {
err = "Collection alredy exists!";
console.log(err);
callback(err.message, result);
return;
}
callback(err, result);
mongodb.close();
});
});
};
// Get Table collection
var getCollection = function (TableName, doc, callback) {
mongoClient.connect(mongoURL, function (err, mongodb) {
//assert.equal(err, null);
if(err){
console.log("unable to connect to db:", mongoURL);
console.log(err);
callback(err.message,null);
return;
}
mongodb.collection(TableName).find(doc).toArray(function(err, result){
if(err){
callback(err.message,null);
return;
}
callback(err, result);
mongodb.close();
});
});
};
var removeCollection = function (TableName, doc, callback) {
mongoClient.connect(mongoURL, function (err, mongodb) {
//assert.equal(err, null);
if(err){
console.log("unable to connect to db:", mongoURL);
console.log(err);
callback(err.message,null);
return;
}
mongodb.collection(TableName).remove(doc)(function(err, result){
if(err){
callback(err.message,null);
return;
}
callback(err, result);
mongodb.close();
});
});
};
// Get Table collection with selected fields and where
var getCollectionFields = function (TableName, doc, fields, callback) {
console.log(TableName, doc, fields);
mongoClient.connect(mongoURL, function (err, mongodb) {
//assert.equal(err, null);
if(err){
console.log("unable to connect to db:", mongoURL);
console.log(err);
callback(err.message,null);
return;
}
mongodb.collection(TableName).find(doc, fields).toArray(function(err, result){
//console.log('result',result);
if(err){
callback(err.message,null);
return;
}
callback(err, result);
mongodb.close();
});
});
};
// Get Table collection top n
var getTop = function (TableName,objSort,limit, doc, callback) {
mongoClient.connect(mongoURL, function (err, mongodb) {
//assert.equal(err, null);
if(err){
console.log("unable to connect to db:", mongoURL);
console.log(err);
callback(err.message,null);
return;
}
mongodb.collection(TableName).find(doc.where, doc.fields).sort(objSort).limit(limit).toArray(function(err, result){
if(err){
callback(err.message,null);
return;
}
callback(err, result);
mongodb.close();
});
});
};
// save Repository in table
var saveRepository = function (repository, callback) {
//console.log("repository", repository);
repositoryMaster = {
name: repository.name,
fields: repository.fields,
collectionName : "Repository_" + repository.name
}
//console.log(repositoryMaster);
getCollection("Repository", {name: repository.name}, function (err, result) {
if (err) {
console.log("Repository get:", err);
callback(err, null);
return;
}
if(result.length > 0){
err="Entity already exists. Please try another name";
console.log(err + ":" + repository.name);
callback(err, null);
return;
}
insertCollection("Repository", repositoryMaster, function (err, result) {
if (err) {
console.log("insert error:", err);
callback(err, null);
return;
}
//console.log(" Repository.data", Repository.data);
insertCollection(repositoryMaster.collectionName, repository.data, function (err, result) {
if (err) {
console.log("insert error:", err);
callback(err, null);
removeCollection("Repository", repositoryMaster,function(err,result){
console.log("remove err, result:",err,result);
})
return;
}
callback(null, "Repository Saved Successfully");
});
});
});
};
module.exports.saveRepository = saveRepository;
// get Repository List
var getRepository = function (config, callback) {
getCollectionFields("Repository", {}, { name: 1, _id: 0 }, function (err, result) {
if (err) {
console.log("getRepository:", err);
callback(err, null);
return;
}
callback(null, result);
});
};
module.exports.getRepository = getRepository;
// get Repository Data keys
var getRepositoryFields = function (repositoryName, callback) {
//TODO: { fields: 1, _id: 0 } is giving parsing error to be debugged
getCollectionFields("Repository", {name: repositoryName}, { _id: 0 }, function (err, result) {
if (err) {
console.log("getRepositoryFields:", err);
callback(err, null);
return;
}
else if(result.length == 0){
err= "Invalid repository name :" + repositoryName;
callback(err, result);
return;
}
//console.log('result',result);
callback(null, result[0].fields);
});
};
module.exports.getRepositoryFields = getRepositoryFields;
// get Source Config
var getSourceConfig = function (config, callback) {
getCollection("SourceMaster", config, function (err, result) {
if (err) {
console.log("getSourceConfig:", err);
callback(err, null);
return;
}
callback(null, result);
});
};
module.exports.getSourceConfig = getSourceConfig;
var getRepositoryData = function (repositoryName, callback) {
getCollectionFields("Repository_"+repositoryName,{}, {_id: 0 }, function (err, result) {
if (err) {
console.log("getRepositoryData:", err);
callback(err, null);
return;
}
callback(err, result);
});
};
module.exports.getRepositoryData = getRepositoryData;
App.config.js
var config = {};
config["mongoServerIP"] = "localhost";
config["mongoServerPort"] = "27017";
config["mongoDBName"] = "DataLoader";
var getConfig = function (keyName) {
return config[keyName];
}
module.exports.getConfig = getConfig;
use in Angular Create Factory and use angular resource
//Create factory for Repository Operation 1.0
izLoader.factory('repository', function ($resource, $http, Upload) {
return $resource('/repository/:cmd', {}, {
repositoryList: { method: 'GET',isArray:false, params: { cmd: 'repositoryList' } }
});
});
call this service get method with query
repository.get({//method: "GET", isArray: false,
"cmd": 'getFields', "repositoryName": config.inputSourceProperties.ddlRepositoryName, isArray: false
}, function (result, headers) {
callback(result.err, result.data);
}, function (err) {
callback("Oops it seems some problem on the server or network connectivity", null);
});
Another method
repository.repositoryList({}, function (result, headers) {
cb(result);
}, function (err) {
console.log('error in getRepositoryList', err);
});