On the server with MongoDB, you can write a basic NodeJS/Express application that reads from Mongo and outputs JSON data. For example, say you have a users
collection in your database:
var MongoClient = require('mongodb').MongoClient
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
// Connection URL
var url = 'mongodb://localhost:27017/yourDatabaseName';
app.get('/users', function(req, res) {
var getUsers = function(db, callback) {
var collection = db.collection('users');
collection.find({}, {password: 0}).toArray(function(err, docs) {
callback(docs);
});
};
MongoClient.connect(url, function(err, db) {
getUsers(db, function(docs) {
res.json(docs);
});
});
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
Then on server #2 you can use jQuery's getJSON() method to access that data from the url server1's IP:3000/users like so:
$.getJSON( "server1IP:3000/users", function( data ) {
var items = [];
$.each( data, function( key, val ) {
items.push( "<li id='" + key + "'>" + val + "</li>" );
});
$( "<ul/>", {
"class": "my-new-list",
html: items.join( "" )
}).appendTo( "body" );
});
As for the other CRUD operations like PUT, DELETE, etc. you can use MongoDB's native NodeJS driver as I did in this example or an ORM like Mongoose