日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

node-mongo-服务器封装

發(fā)布時(shí)間:2025/6/17 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 node-mongo-服务器封装 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
分為三個(gè)文件 mongo.js基本的封裝了下mongo數(shù)據(jù)庫(kù)操作 workmongo.js 里面有路由和解析操作(可以根據(jù)具體業(yè)務(wù)進(jìn)行擴(kuò)充) mainmongo.js 服務(wù)器相關(guān)調(diào)用例子:查詢數(shù)據(jù) http://127.0.0.1:2323/get?k=q&n=data&c=wck&w={"y":"y"} 添加數(shù)據(jù) http://127.0.0.1:2323/post?k=i&n=data&c=wck 具體的內(nèi)容,在post的body里面,采用標(biāo)準(zhǔn)json格式就好---------------------------------------------------- mongo.js const { MongoClient, ObjectId } = require('mongodb'); const mongourl = "mongodb://localhost:27017/";const findMongo = (dbname, collection, where, req, res) => {MongoClient.connect(mongourl, { useNewUrlParser: true, useUnifiedTopology: true }, function (err, client) {if (err) throw err;const db = client.db(dbname);db.collection(collection).find(where).sort({ uptime: -1 }).toArray(function (err, datas) {if (err) throw err;res.writeHead(200, { "Content-Type": "text/plain; charset=utf8" });res.end(JSON.stringify(datas));//client.close();});});return; }const insertMongo = async (dbname, collection, newdatas, req, res) => {MongoClient.connect(mongourl, { useNewUrlParser: true, useUnifiedTopology: true }, function (err, client) {if (err) throw err;const db = client.db(dbname);db.collection(collection).insertMany(newdatas, function (err, datas) {if (err) throw err;res.writeHead(200, { "Content-Type": "text/plain; charset=utf8" });res.end(JSON.stringify(datas));//client.close();});});return; }module.exports = {findMongo,insertMongo };---------------------------------------------------- workmongo.js const url = require('url'); const mongo = require('./mongo'); const querystring = require('querystring');//get 獲取數(shù)據(jù)/查詢 const work_get = async (req, res) => {const params = url.parse(req.url, true).query;try {switch (params.k) {case 'q': {if (params.n && params.c) {//基本查詢 條件里不能加 ObjectId 如果需要的話可以單獨(dú)寫//dbname collection where//http://127.0.0.1:2323/get?k=q&n=data&c=wck&w={"y":"y"}mongo.findMongo(params.n, params.c, JSON.parse(params.w), req, res);}} break;default: {res.writeHead(200, { "Content-Type": "text/plain; charset=utf8" });res.write("Wow");res.end();}; break;}} catch (e) {datas = null;} }//post 創(chuàng)建數(shù)據(jù) const work_post = async (req, res) => {const params = url.parse(req.url, true).query;let postdata = '';req.on('data', function (chunk) {postdata += chunk;});req.on('end', async function () {postdataobj = querystring.parse(postdata);try {switch (params.k) {case 'i': {if (params.n && params.c) {//插入一條數(shù)據(jù)//dbname collection where//http://127.0.0.1:2323/post?k=i&n=data&c=wck 具體的內(nèi)容,在post的body里面,采用標(biāo)準(zhǔn)json格式就好const postdataobjarr = [postdataobj];mongo.insertMongo(params.n, params.c, postdataobjarr, req, res);}} break;default:{res.writeHead(200, { "Content-Type": "text/plain; charset=utf8" });res.write("Wow");res.end();}; break;}} catch (e) {datas = null;}}); }//其他的各種增刪改查 就按照上面的思路 再結(jié)合具體業(yè)務(wù)場(chǎng)景 慢慢寫就好。 //建議 表轉(zhuǎn)化 查詢用 get 增加用 post 更改用 put 刪除的話隨意把。 //注意url的最大長(zhǎng)度問題。module.exports = {work_get,work_post };---------------------------------------------------- mainmongo.js const http = require('http'); var url = require("url"); const workmongo = require('./workmongo');const route = async (req, res) => {//console.log("url.pathname:" + url.parse(req.url).pathname);switch (url.parse(req.url).pathname) {case "/get": {workmongo.work_get(req, res);}; break;case "/post": {workmongo.work_post(req, res);}; break;default: {res.end();} break;} }const main = async () => {http.createServer(function (req, res) {route(req, res);}).listen(2323); };main();

?

總結(jié)

以上是生活随笔為你收集整理的node-mongo-服务器封装的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。