[英]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'. 对
http://localhost:8080/?name=foo
的GET请求将导致params具有值,属性'name'='foo'。
If you are trying to insert a document with a property 'name', eg {name: 'foo', property2: 'param2', etc...}
into your mongoDB collection, you will want to implement a mongoDB insert into your collection, NOT a find. 如果您尝试将具有属性“name”的文档(例如
{name: 'foo', property2: 'param2', etc...}
插入到mongoDB集合中,则需要在集合中实现mongoDB插入,不是找到。
If you want to do a find, but query by {name: 'foo'}, you will need to pass that as a parameter to find(). 如果你想进行查找,但是通过{name:'foo'}进行查询,则需要将其作为参数传递给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 我建议你看看mongoDB驱动程序文档,了解有关find和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. 如果你只需要返回模拟数据,你可以在命令行中打开mongo shell,$
mongo
,使用TestApp数据库use TestApp
,然后执行db.ObjectCollection.insert({...your document here...})
用数据填充数据库,因此服务器的查询可以返回一些文档。 MongoShell commands are also documented on the MongoDB website. MongoDB命令也记录在MongoDB网站上。
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.