Node + MongoDB Simple implementation of its own query interface

Source: Internet
Author: User

Not much to say directly on the code

Const HTTP = require (' http '); const DB = require (' MongoDB '). Db;const Connection = require (' MongoDB '). Connection;const Server = require (' MongoDB '). Server;const Query2 = require (' querystring '); var url=require (' url '); let settings = {cookiesecret: ' Shop ', db: ' Local ', ho ST: ' localhost ', port:27017}let shopping = new Db (settings.db, New Server (Settings.host,settings.port), {safe:true}); VA R Server = Http.createserver (function (req,res) {var arg=url.parse (req.url). query; var namevalue=query2.parse (ARG) [' Funno ']//var cc = Query.parse (req);//var data = "";//Res.on (' Data ', function (chunk) {//Data + = chunk;//})//. On ("En  D ", function () {/////To Data transcoding//}); if (Namevalue = = ' 001 ') {let param = {pdtid:1,} getone (Param.pdtid,function (err,post) {var data = JSO      N.stringify (POST);    Res.writehead ($, {' Content-type ': ' Application/json ',});      Res.setheader ({' Access-control-allow-origin ': ' * '});  Res.write (data, ' UTF-8 ');//Send the appropriate content to the requesting client, data is buffer or string, encoding is encoded    Res.end ();      End the response, informing the user that all the send has completed, when all the content to be returned is sent out})}) function GetOne (pdtid,callback) {Shopping.open (function (err,db) {if (err) {    Return callback (ERR);          }//Open the database and find the data db.collection (' Chen ', function (err,collection) {if (err) {shopping.close ();        Return callback (ERR); }//Query Collection.findone according to the criteria ({"Pdtid": Pdtid,},function (err,data) {Shopping.clo          SE ();          if (ERR) {return callback (ERR);  }//The document to be traced resolves//doc.post = markdown.tohtml (doc.post);  var cc = []; Cc.push (data) callback (NULL,CC)//Return to query article})})}server.listen (8081);

As in the code above, connect to the local database by setting the database. Then through QueryString parsing query conditions, here the query conditions I wrote dead, Pdtid = 001, and then through the operation of the database, the data returned.

The browser output address, you can see the data.

Node + MongoDB Simple implementation of its own query interface

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.