diff --git a/app.js b/app.js index 8a73fde..6bac8cd 100644 --- a/app.js +++ b/app.js @@ -1,13 +1,21 @@ const Koa = require("koa"); const requter = require("./bin/router.js"); +jwt = require('jsonwebtoken'); +dbs = require("./bin/mongodb.js")('mongodb://localhost:27017/',"myblog") // console.log(requter) // import requter from "./bin/router" const app = new Koa(); +dbs.add('a','a').then((res)=>{ + console.log(res) +}).catch((err)=>{ + console.log(err) +}) + app.use(async (ctx,next)=>{ // await next(); console.log(ctx.url) // ctx.response.body = {a:1}; - next() + await next() console.log("执行") }); diff --git a/bin/mongodb.js b/bin/mongodb.js index e69de29..fcde83a 100644 --- a/bin/mongodb.js +++ b/bin/mongodb.js @@ -0,0 +1,177 @@ +var MongoClient = require('mongodb').MongoClient; +var dburl; +let add = (set,data,url = dburl) =>{ + return new Promise((resolve, reject) => { + MongoClient.connect(url.url, { useNewUrlParser: true }, function(err, db) { + if (err) throw err; + var dbo = db.db(url.dbname); + // var myobj = { name: "菜鸟教程", url: "www.runoob" }; + try { + dbo.collection(set).insertOne(data, function(err, res) { + if (err) throw err; + // console.log("文档插入成功"); + db.close(); + resolve({code:0}); + }); + } catch (error) { + reject({code:1,error}) + } + + }); + }) +} +let adds = (set,data,url = dburl) =>{ + return new Promise((resolve, reject) => { + MongoClient.connect(url.url, { useNewUrlParser: true }, function(err, db) { + if (err) throw err; + var dbo = db.db(url.dbname); + // var myobj = { name: "菜鸟教程", url: "www.runoob" }; + try { + dbo.collection(set).insertMany(data, function(err, res) { + if (err) throw err; + // console.log("文档插入成功"); + db.close(); + resolve({code:0,num:res.insertedCount}); + }); + } catch (error) { + reject({code:1,error}) + } + + }); + }) +} +let remove = (set,where,url = dburl) =>{ + return new Promise((resolve, reject) => { + MongoClient.connect(url.url, { useNewUrlParser: true }, function(err, db) { + if (err) throw err; + var dbo = db.db(url.dbname); + // var myobj = { name: "菜鸟教程", url: "www.runoob" }; + try { + dbo.collection(set).deleteOne(where, function(err, obj) { + if (err) throw err; + // console.log("文档插入成功"); + db.close(); + resolve({code:0}); + }); + } catch (error) { + reject({code:1,error}) + } + + }); + }) +} +let removes = (set,where,url = dburl) =>{ + return new Promise((resolve, reject) => { + MongoClient.connect(url.url, { useNewUrlParser: true }, function(err, db) { + if (err) throw err; + var dbo = db.db(url.dbname); + // var myobj = { name: "菜鸟教程", url: "www.runoob" }; + try { + dbo.collection(set).deleteMany(where, function(err, obj) { + if (err) throw err; + // console.log("文档插入成功"); + db.close(); + resolve({code:0,num:obj.result.n}); + }); + } catch (error) { + reject({code:1,error}) + } + + }); + }) +} +let find = (set,where = {},page = 0,num = 0,url = dburl) =>{ + return new Promise((resolve, reject) => { + if(num == 0){ + MongoClient.connect(url.url, { useNewUrlParser: true }, function(err, db) { + if (err) throw err; + var dbo = db.db(url.dbname); + // var myobj = { name: "菜鸟教程", url: "www.runoob" }; + try { + dbo.collection(set). find(where).toArray(function(err, result) { // 返回集合中所有数据 + if (err) throw err; + // console.log(result); + resolve({code:0,data:result}); + db.close(); + }); + } catch (error) { + reject({code:1,error}) + } + + }); + }else{ + MongoClient.connect(url.url, { useNewUrlParser: true }, function(err, db) { + if (err) throw err; + var dbo = db.db(url.dbname); + // var myobj = { name: "菜鸟教程", url: "www.runoob" }; + try { + dbo.collection(set). find(where).skip(num * page).limit(num).toArray(function(err, result) { // 返回集合中所有数据 + if (err) throw err; + // console.log(result); + resolve({code:0,data:result}); + db.close(); + }); + } catch (error) { + reject({code:1,error}) + } + + }); + } + + }) +} +let update = (set,where = {},data,url = dburl) =>{ + return new Promise((resolve, reject) => { + MongoClient.connect(url.url, { useNewUrlParser: true }, function(err, db) { + if (err) throw err; + var dbo = db.db(url.dbname); + // var myobj = { name: "菜鸟教程", url: "www.runoob" }; + data = {$set:data} + try { + dbo.collection(set).updateOne(where, data, function(err, res) { + if (err) throw err; + // console.log(result); + resolve({code:0}); + db.close(); + }); + } catch (error) { + reject({code:1,error}) + } + + }); + }) +} +let updates = (set,where = {},data,url = dburl) =>{ + return new Promise((resolve, reject) => { + MongoClient.connect(url.url, { useNewUrlParser: true }, function(err, db) { + if (err) throw err; + var dbo = db.db(url.dbname); + // var myobj = { name: "菜鸟教程", url: "www.runoob" }; + data = {$set:data} + try { + dbo.collection(set).updateMany(where, data, function(err, res) { + if (err) throw err; + // console.log("文档插入成功"); + db.close(); + resolve({code:0,num:res.result.nModified}); + }); + } catch (error) { + reject({code:1,error}) + } + + }); + }) +} + +module.exports = function (url,dbname){ + dburl = {url,dbname}; + return{ + add, + adds, + remove, + find, + update, + updates, + removes + } +} \ No newline at end of file diff --git a/bin/router.js b/bin/router.js index 6f0daf6..64f41b1 100644 --- a/bin/router.js +++ b/bin/router.js @@ -17,16 +17,16 @@ function addMapping(router, mapping) { } } -function addControllers(router,controllers_dir) { +function addControllers(router, controllers_dir) { let ostype = os.type() == "Windows_NT" let dir; - if(ostype){ - dir = __dirname.replace("\\bin","") - }else{ - dir = __dirname.replace("/bin","") + if (ostype) { + dir = __dirname.replace("\\bin", "") + } else { + dir = __dirname.replace("/bin", "") } - - var files = fs.readdirSync(dir +"/"+ controllers_dir); + + var files = fs.readdirSync(dir + "/" + controllers_dir); var js_files = files.filter((f) => { return f.endsWith('.js'); }); diff --git a/controllers/login.js b/controllers/login.js index fcb8023..2461120 100644 --- a/controllers/login.js +++ b/controllers/login.js @@ -1,10 +1,37 @@ + var login = async (ctx, next) => { // var name = ctx.params.name; // ctx.response.body = `