xuebaoxin #12

Manually merged
theluyuan merged 2 commits from xuebaoxin into master 2019-11-12 23:31:19 +08:00
6 changed files with 265 additions and 10 deletions

4
app.js
View File

@ -1,5 +1,7 @@
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();
@ -7,7 +9,7 @@ app.use(async (ctx,next)=>{
// await next();
console.log(ctx.url)
// ctx.response.body = {a:1};
next()
await next()
console.log("执行")
});

View File

@ -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
}
}

View File

@ -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');
});

View File

@ -1,10 +1,37 @@
var login = async (ctx, next) => {
// var name = ctx.params.name;
// ctx.response.body = `<h1>Hello, ${name}!</h1>`;
ctx.response.body="这是login"
next()
};
var loginup = async (ctx,next) => {
// ctx.body="这是login,post,name:" + ctx.request.body.name
// console.log("post")
//返回token
console.log(ctx.request.body.name == 'admin' , ctx.request.body.pwd == "123456")
if(ctx.request.body.name == 'admin' && ctx.request.body.pwd == "123456"){
const token = jwt.sign({
name: ctx.request.body.name,
_id: 1
}, 'my_token', { expiresIn: '2h' });
// jwt.verify("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ8.eyJuYW1lIjoiYWRtaW4iLCJfaWQiOjEsImlhdCI6MTU3MzU1Mjg1MCwiZXhwIjoxNTczNTYwMDUwfQ.TmAMeDq58F9Tt6gIQ9uU_FUVk4CEOKM90tvzymJo4Oc", 'my_token', function (err, data) {
// if (err) console.log(err)
// console.log('解析的数据', data)
// })
ctx.body={code:0,token}
}else{
ctx.body={
code:1,
msg:"密码错误"
}
}
next()
}
module.exports = {
'GET /login': login
'GET /login': login,
'POST /login' : loginup
};

View File

@ -9,6 +9,7 @@
"koa-bodyparser": "^4.2.1",
"koa-jwt": "^3.6.0",
"koa-router": "^7.4.0",
"mongodb": "^3.3.4",
"nunjucks": "^3.2.0"
}
}

View File

@ -151,6 +151,11 @@ braces@^2.3.1, braces@^2.3.2:
split-string "^3.0.2"
to-regex "^3.0.1"
bson@^1.1.1:
version "1.1.3"
resolved "https://registry.yarnpkg.com/bson/-/bson-1.1.3.tgz#aa82cb91f9a453aaa060d6209d0675114a8154d3"
integrity sha512-TdiJxMVnodVS7r0BdL42y/pqC9cL2iKynVwA0Ho3qbsQYr428veL3l7BQyuqiw+Q5SqqoT0m4srSY/BlZ9AxXg==
buffer-equal-constant-time@1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz#f8e71132f7ffe6e01a5c9697a4c6f3e48d5cc819"
@ -1024,6 +1029,11 @@ media-typer@0.3.0:
resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748"
integrity sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=
memory-pager@^1.0.2:
version "1.5.0"
resolved "https://registry.yarnpkg.com/memory-pager/-/memory-pager-1.5.0.tgz#d8751655d22d384682741c972f2c3d6dfa3e66b5"
integrity sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==
methods@^1.0.1:
version "1.1.2"
resolved "https://registry.yarnpkg.com/methods/-/methods-1.1.2.tgz#5529a4d67654134edcc5266656835b0f851afcee"
@ -1107,6 +1117,17 @@ mkdirp@^0.5.0, mkdirp@^0.5.1:
dependencies:
minimist "0.0.8"
mongodb@^3.3.4:
version "3.3.4"
resolved "https://registry.yarnpkg.com/mongodb/-/mongodb-3.3.4.tgz#f52eec4a04005101e63715d4d1f67bf784fa0aef"
integrity sha512-6fmHu3FJTpeZxacJcfjUGIP3BSteG0l2cxLkSrf1nnnS1OrlnVGiP9P/wAC4aB6dM6H4vQ2io8YDjkuPkje7AA==
dependencies:
bson "^1.1.1"
require_optional "^1.0.1"
safe-buffer "^5.1.2"
optionalDependencies:
saslprep "^1.0.0"
ms@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
@ -1421,6 +1442,19 @@ repeat-string@^1.6.1:
resolved "https://registry.yarnpkg.com/repeat-string/-/repeat-string-1.6.1.tgz#8dcae470e1c88abc2d600fff4a776286da75e637"
integrity sha1-jcrkcOHIirwtYA//Sndihtp15jc=
require_optional@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/require_optional/-/require_optional-1.0.1.tgz#4cf35a4247f64ca3df8c2ef208cc494b1ca8fc2e"
integrity sha512-qhM/y57enGWHAe3v/NcwML6a3/vfESLe/sGM2dII+gEO0BpKRUkWZow/tyloNqJyN6kXSl3RyyM8Ll5D/sJP8g==
dependencies:
resolve-from "^2.0.0"
semver "^5.1.0"
resolve-from@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-2.0.0.tgz#9480ab20e94ffa1d9e80a804c7ea147611966b57"
integrity sha1-lICrIOlP+h2egKgEx+oUdhGWa1c=
resolve-url@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/resolve-url/-/resolve-url-0.2.1.tgz#2c637fe77c893afd2a663fe21aa9080068e2052a"
@ -1460,12 +1494,19 @@ safe-regex@^1.1.0:
resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a"
integrity sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==
saslprep@^1.0.0:
version "1.0.3"
resolved "https://registry.yarnpkg.com/saslprep/-/saslprep-1.0.3.tgz#4c02f946b56cf54297e347ba1093e7acac4cf226"
integrity sha512-/MY/PEMbk2SuY5sScONwhUDsV2p77Znkb/q3nSVstq/yQzYJOH/Azh29p9oJLsl3LnQwSvZDKagDGBsBwSooag==
dependencies:
sparse-bitfield "^3.0.3"
sax@^1.2.4:
version "1.2.4"
resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9"
integrity sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==
semver@^5.3.0, semver@^5.6.0:
semver@^5.1.0, semver@^5.3.0, semver@^5.6.0:
version "5.7.1"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7"
integrity sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==
@ -1546,6 +1587,13 @@ source-map@^0.5.6:
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.5.7.tgz#8a039d2d1021d22d1ea14c80d8ea468ba2ef3fcc"
integrity sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w=
sparse-bitfield@^3.0.3:
version "3.0.3"
resolved "https://registry.yarnpkg.com/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz#ff4ae6e68656056ba4b3e792ab3334d38273ca11"
integrity sha1-/0rm5oZWBWuks+eSqzM004JzyhE=
dependencies:
memory-pager "^1.0.2"
split-string@^3.0.1, split-string@^3.0.2:
version "3.1.0"
resolved "https://registry.yarnpkg.com/split-string/-/split-string-3.1.0.tgz#7cb09dda3a86585705c64b39a6466038682e8fe2"