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