如何使用客户端JavaScript从MongoDB和一个节点查找和返回数据。没有框架的js服务器?

时间:2022-03-16 04:29:16

I have read all the questions on SO that I could find. They all use Express, Mongoose or they leave something out. I understand that Node.js is the server. I understand the MongoDB require is the driver the Node.js server uses to open a connection to the MongoDB. Then, on the server, I can do (from the documentation):

我把所有的问题都读了,以便能找到。他们都用Express, Mongoose或者省略一些东西。我知道节点。js是一个服务器。我知道MongoDB需要的是驱动节点。js服务器用于打开到MongoDB的连接。然后,在服务器上,我可以做(从文档):

var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var ObjectId = require('mongodb').ObjectID;
var url = 'mongodb://localhost:27017/test';

var findRestaurants = function(db, callback) {
   var cursor =db.collection('restaurants').find( );
   cursor.each(function(err, doc) {
      assert.equal(err, null);
      if (doc != null) {
         console.dir(doc);
      } else {
         callback();
      }
   });
};
// Connect to the db
MongoClient.connect(url, function(err, db) {
  assert.equal(null, err);
  findRestaurants(db, function() {  //I don't want to do this as soon as the server starts
      db.close();
  });
});

//if I put findRestaurant here,
function findRestaurant(data){

}

How do I call it from the client? I do not want to find data as soon as I start the server. I realize those are examples, but what I cannot find is a way where the client requests some data and where the Node.js server returns it.

如何从客户那里调用?我不希望在启动服务器时就找到数据。我知道这些都是示例,但是我找不到客户端请求一些数据和节点在哪里的方法。js服务器返回它。

I have seen close examples using jQuery, Angular on the client, and then Express, Mongoose, Meteor, , etc.

我曾见过使用jQuery的close示例,在客户端有角度,然后表示,Mongoose,流星,等等。

All I want to understand is how I make this request from the client's browser. I can do that with XMLhttpRequest(), so I can put that part together, I believe. But, any example is appreciated.

我想知道的是我如何从客户端浏览器发出这个请求。我可以用XMLhttpRequest()来实现这一点,我相信我可以把这部分结合起来。但是,任何一个例子都是值得赞赏的。

But what is waiting on the Node.js side of things (how do I set up my function to be called once the server is listening)?

但是节点上正在等待的是什么。js方面(如何设置在服务器监听时调用的函数)?

How do I create a function on the server side, maybe "GetRestaurants" and have that return the data it gets using find()?

我如何在服务器端创建一个函数,可能是“getreants”,并返回使用find()的数据?

I cannot find this information, this simple, anywhere. Is it too complicated to do the example without a framework?

我在任何地方都找不到这么简单的信息。在没有框架的情况下做这个例子是不是太复杂了?

I do not wish to copy and paste from something using Express, etc. without understanding what's going on. Most explanations never say, this goes on the Node.js side. This is client. I know I am expected to do my own research, but I am not putting it together, too used to RDBMSes, IIS, Apache, PHP, and so on.

我不希望在不了解情况的情况下,用Express等方法复制和粘贴某样东西。大多数解释从来没有说过,这是在节点上。js的一面。这是客户端。我知道我需要自己做研究,但我没有把它放在一起,我太习惯于rdbms、IIS、Apache、PHP等等。

I believe I have a fundamental misunderstanding of what's going on in the paradigm.

我相信我对范例中发生的事有一个根本的误解。

Please. No REST API creation, no frameworks of any kind on Node.js other than using the MongoDB library (unless there is an absolute requirement), not even jQuery, Angular, Jade, or anything else for the client side, straight up JavaScript on all sides.

请。没有REST API的创建,节点上没有任何框架。除了使用MongoDB库(除非有绝对的需求)之外的js,甚至包括jQuery、angle、Jade或其他客户机端,所有方面都直接使用JavaScript。

I have seen questions like this,

我见过这样的问题,

How to display data from MongoDB to the frontend via Node.js without using a framework

如何通过节点向前端显示MongoDB的数据。不使用框架的js

But they do not show what I am asking. They do it all at once, as soon as the database connects. What if I want to do a delete or insert or find? There are many SO questions like this, but I have not hit the one that shows what I am looking for.

但他们没有显示出我的要求。一旦数据库连接起来,它们就会立即执行所有操作。如果我想要删除、插入或查找呢?有很多这样的问题,但是我还没有找到我想要的。

1 个解决方案

#1


2  

This should give the guidance. Once you go to a browser and type http://localhost:5155 the callback function (request, response) { will be called and the request to db will be made. Make sure you get response and then start working on the client side code:

这应该提供指导。访问浏览器并输入http://localhost:5155之后,将调用回调函数(request、response){并请求db。确保您得到了响应,然后开始处理客户端代码:

const http = require('http');
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
const url = 'mongodb://localhost:27017/test';

const server = http.createServer(function (request, response) {
    getData(function (data) {
        response.end(data);
    });
});

function getData(callback) {
    // Connect to the db
    MongoClient.connect(url, function (err, db) {
        assert.equal(null, err);
        findRestaurants(db, function (data) {
            db.close();
            callback(data);
        });
    });

    const findRestaurants = function (db, callback) {
        const cursor = db.collection('restaurants').find();
        const data = [];
        cursor.each(function (err, doc) {
            assert.equal(err, null);
            data.push(doc);
            if (doc === null) {
                callback(data);
            }
        });
    };
}

server.listen(5155);

#1


2  

This should give the guidance. Once you go to a browser and type http://localhost:5155 the callback function (request, response) { will be called and the request to db will be made. Make sure you get response and then start working on the client side code:

这应该提供指导。访问浏览器并输入http://localhost:5155之后,将调用回调函数(request、response){并请求db。确保您得到了响应,然后开始处理客户端代码:

const http = require('http');
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
const url = 'mongodb://localhost:27017/test';

const server = http.createServer(function (request, response) {
    getData(function (data) {
        response.end(data);
    });
});

function getData(callback) {
    // Connect to the db
    MongoClient.connect(url, function (err, db) {
        assert.equal(null, err);
        findRestaurants(db, function (data) {
            db.close();
            callback(data);
        });
    });

    const findRestaurants = function (db, callback) {
        const cursor = db.collection('restaurants').find();
        const data = [];
        cursor.each(function (err, doc) {
            assert.equal(err, null);
            data.push(doc);
            if (doc === null) {
                callback(data);
            }
        });
    };
}

server.listen(5155);