First build back-end servers through Nodejs and MongoDB
First, start the MongoDB server under the Windows platform
1. Enter the installation directory of MongoDB and go to the bin directory to start Mongod
2. Set up Mongodb\data folder as data storage directory in D disk
Mongod.exe--dbpath D:\mongodb\data
Second, set up JS file, and start by Nodejs
1. When sending 01 instructions to the server, the server reads the data from MongoDB and returns
var HOST = "localhost"; var PORT = 6969;var Dgram = require (' Dgram '); var server_udp = Dgram.createsocket (' udp4 '); var mongo= Require ("MongoDB"); var db_port=27017;var server_database=new MONGO. Server (Host,db_port,{auto_reconnect:true}); var db=new mongo. DB ("Test", server_database,{safe:true});d B.open (function (err,db) {///Connection database if (err) {Console.log ("error"); throw err; } else{db.collection ("Student", function (err,collection) {Collection.insert ({name: "Jinjian", score : "+"}, function (Err,docs) {console.log (docs); Db.close (FALSE); }); }); }}); Server_udp.on (' Listening ', function () {var address = server_udp.address (); Console.log ("Server Listening" + address.address + ":" + address.port); Console.log ("\ n");}); Server_udp.on (' message ', function (message,remote) {Console.log ("" +message); Switch ("+message") {case ("n"): {Console.log ("receive 01"); Db.open (function (ERR,DB) {db.collection ("Student", function (err,collection) {if (err) throw err; else{collection.find ({}). ToArray (function (Err,docs) {if (err) throw err; else{var str = json.stringify (docs); Server_udp.send (str,0,str.length,6967,remote.address); Console.log (str); Db.close (FALSE); } }); } }); }); Break } case ("Console.log"): {("Receive 02"); Break } case ("" "): {Console.log (" receive 03 "); Break }} console.log (Remote.address + ":" + message); Console.log ("\ n"); }); Server_udp.bind (Port,host);
III. writing the MFC interface
1, MFC accepted the data after parsing code as follows (the program uses the Jsoncpp-json Data Analysis Library)
Json::reader Reader; Json::value Json_object;int len = strlen ((char *) lParam), char* temp = new char [len];memcpy (temp, (char *) lparam,len); Reader.parse (temp, json_object)) return 0;int size = Json_object.size (); const Json::value arrayobj = json_object;for (int i=0; I<arrayobj.size (); i++) {String id = arrayobj[arrayobj.size () -1-i]["number"].asstring (); string name= arrayobj[arrayobj.size () -1-i][" Name "].asstring (); string score = Arrayobj[arrayobj.size () -1-i][" score "].asstring (); int n = m_list. InsertItem (0,id.c_str ()); m_list. Setitemtext (N,1,name.c_str ()); m_list. Setitemtext (N,2,score.c_str ());//fprintf (Pfoutput, "%s\n", Id.c_str ()),//fprintf (Pfoutput, "%s\n", Firstname.c_str ());//fprintf (Pfoutput, "%s\n", Username.c_str ());}
Iv. test Read the database, MFC sends the "01" instruction to the database and displays the returned data in the list
1. Insert data into MongoDB. Enter the bin directory of the MongoDB program
D:\mongodb\bin>mongo
>use Test
Db.student.insert ({"n": "X1", "Up", "X2",.....});
2 MFC Read effect is as follows
Read and write operations on the MFC+MONGODB+NODEJS database