Node js : receive data from client

I have a problem. Send data from the client to the server, to the database. But the server does not see the data from the client, if you do this:

var http = require("http");
var url = require("url");
var Db = require ("mongodb").Db;
var Server = require("mongodb").Server;
function start () {
'use strict';
function onRequest (request, response) {
    'use strict';
    var db = new Db ("TestApp", new Server ("127.0.0.1", 27017, {}));
    response.writeHead(200, {"Content-Type": "application/json", "Access-Control-Allow-Origin": "*"});
    db.open (function (err, db) {
        db.collection ('ObjectCollection', function (err, collection) {
                var url_string = request.url;
                var params = url.parse(url_string, true).query;
                console.log(params["name"]);
            collection.find().toArray (function (err, docs) {
                console.log (docs);
                response.write(JSON.stringify(docs));

                response.end();
            });
        });
    });
}

http.createServer(onRequest).listen(8080);
console.log ('Server has started...')
}
exports.start = start;

And this is how he sees, but since I can not add data to the database

var http = require("http");
var url = require("url");
var Db = require ("mongodb").Db;
var Server = require("mongodb").Server;
function start () {
'use strict';
function onRequest (request, response) {
    'use strict';
    var db = new Db ("TestApp", new Server ("127.0.0.1", 27017, {}));
    response.writeHead(200, {"Content-Type": "application/json", "Access-Control-Allow-Origin": "*"});
    db.open (function (err, db) {
        db.collection ('ObjectCollection', function (err, collection) {
            collection.find().toArray (function (err, docs) {
                console.log (docs);
                response.write(JSON.stringify(docs));
                var url_string = request.url;
                var params = url.parse(url_string, true).query;
                console.log(params["name"]);
                response.end();
            });
        });
    });
}

http.createServer(onRequest).listen(8080);
console.log ('Server has started...')
}
exports.start = start;

Help me, please :)

Not sure what you are trying to do, but I will do my best to give you advice.

Your code for parsing the query parameters looks fine. A GET request to http://localhost:8080/?name=foo will result in params having values, with property 'name' = 'foo'.

If you are trying to insert a document with a property 'name', e.g. {name: 'foo', property2: 'param2', etc...} into your mongoDB collection, you will want to implement a mongoDB insert into your collection, NOT a find.

If you want to do a find, but query by {name: 'foo'}, you will need to pass that as a parameter to find().

I suggest you take a look at the mongoDB driver documentation for more on find and insert: http://mongodb.github.com/node-mongodb-native/api-articles/nodekoarticle1.html


If you just need mock data to return, you can alternatively open up the mongo shell in the command line, $mongo, use the TestApp database use TestApp, and do a db.ObjectCollection.insert({...your document here...}) to populate your database with data, so your server's query can return some documents. MongoShell commands are also documented on the MongoDB website.

Hope it helps.