简体   繁体   中英

How to make CRUD operations on a remote Mongodb with Nodejs

I recently started to play around with NodeJS - all I know is that it's a server side technology. What I did and want to accomplish are as following:

I have a MongoDB running on a remote server. I am using nodejs mongodb driver , and by simply doing the following I can connect to the database and just lets say create a document:

// main.js
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://remote_url:27017/mymongo', function(err, db) {
    var document = {a:"1", b:"2"};
    db.collection('collection').insert(document, function(err, records) {
        if (err) throw err;
    }
}

As you know, the code above requires a console call such: node main.js , however I have a HTML5 frontend with several text fields, and I want to pass the fields to my database with a simple button click event. My questions are:

Is it really stupid if I directly connect to remote mongodb as above? Can I call the script from my HTML page? If I can, then what are the drawbacks compared to redesigning it into a client-server structure?

Finally, I think the right practice to accomplish above is to create an http server with nodejs on the remote server which passes client's requests to the mongodb driver. Am I right?

You could try building a REST API to interact with the MongoDB server(s) using vanilla NodeJS or your choice of quite a few additional frameworks. You might try baucis *.

*Disclaimer: I am the main author of baucis

You should use a REST interface for MongoDB. I like sleepy.mongoose a lot. It runs on python and can take a minute to set up, but is well worth the effort.

Here is a blog by the author which helped get me started.

Is it really stupid if I directly connect to remote mongodb as above?

No, using the native MongoDB driver is pretty standard. However, instead of connecting and then immediately interacting with your database, I'd structure the application so that you connect and then wait for HTTP calls or some other function to interact with the database.

Can I call the script from my HTML page?

Absolutely. In your node.js application, I would build in a web server that listens for certain HTTP calls. In your HTML, provide links or forms to GET, POST, etc. the web server that your application is listening on.

For example, your front-end would look like maybe a <form action="/document/add" method="post"> . Then, keep main.js as your back-end code running on node.js, but modify it to listen for a POST call to /document/add . When a call to that URL comes in, run the insert code with the POSTed form data.

You could also create an AJAX solution to listen for form submission and submit the POST in the background, wait for a response, and update the page accordingly.

What are the drawbacks compared to redesigning it into a client-server structure?

Advantages and drawbacks are going to be very specific to the type of application you want to create.

I think the right practice to accomplish above is to create an http server with nodejs on the remote server which passes client's requests to the mongodb driver. Am I right?

You are correct. That is pretty standard practice for using node.js and MongoDB. I'm going to recommend Express for creating your API to interface with the database. It provides features such as URL routing right out of the box. However, you can build your own platform, or use any other one that works for your application/environment.

这是部署到Heroku的演示应用程序, http: //nodejs-crud.herokuapp.com/,教程链接为http://codeforbrowser.com/blog/crud-operations-in-node-js-and-mongodb/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM