This commit is contained in:
luyuan 2021-02-24 09:06:18 +08:00
parent 2ef9823b2f
commit f458c472f7
Signed by: theluyuan
GPG Key ID: A7972FD973317FF3
3 changed files with 37 additions and 5 deletions

7
app.js
View File

@ -1,6 +1,6 @@
const Koa = require("koa"); const Koa = require("koa");
const requter = require("./bin/router.js"); const requter = require("./bin/router.js");
dbs = require("./bin/mongodb.js")('mongodb://localhost:27017/ceshikaoshi',"ceshikaoshi") dbs = require("./bin/mongodb.js")('mongodb://localhost:27017/',"ceshikaoshi")
// console.log(requter) // console.log(requter)
// import requter from "./bin/router" // import requter from "./bin/router"
const app = new Koa(); const app = new Koa();
@ -41,8 +41,13 @@ app.use(async (ctx, next) => {
// getResponseHeader('myData')可以返回我们所需的值 // getResponseHeader('myData')可以返回我们所需的值
//https://www.rails365.net/articles/cors-jin-jie-expose-headers-wu //https://www.rails365.net/articles/cors-jin-jie-expose-headers-wu
ctx.set("Access-Control-Expose-Headers", "myData"); ctx.set("Access-Control-Expose-Headers", "myData");
if(ctx.method == "OPTIONS"){
ctx.body = ""
}else{
await next(); await next();
}
}) })
app.use(async (ctx,next)=>{ app.use(async (ctx,next)=>{
// await next(); // await next();

View File

@ -6,11 +6,11 @@ function addMapping(router, mapping) {
if (url.startsWith('GET ')) { if (url.startsWith('GET ')) {
var path = url.substring(4); var path = url.substring(4);
router.get(path, mapping[url]); router.get(path, mapping[url]);
// console.log(`register URL mapping: GET ${path}`); console.log(`register URL mapping: GET ${path}`);
} else if (url.startsWith('POST ')) { } else if (url.startsWith('POST ')) {
var path = url.substring(5); var path = url.substring(5);
router.post(path, mapping[url]); router.post(path, mapping[url]);
// console.log(`register URL mapping: POST ${path}`); console.log(`register URL mapping: POST ${path}`);
} else { } else {
console.log(`invalid URL: ${url}`); console.log(`invalid URL: ${url}`);
} }

View File

@ -76,8 +76,35 @@ let findshop = async (ctx, next) => {
ctx.body = await dbs.find("shop") ctx.body = await dbs.find("shop")
} }
let logins = async(ctx,next)=>{
let {user , pwd} = ctx.request.body
console.log(123)
if(user == "admin" && pwd == "123"){
ctx.body = JSON.stringify({
code: 200,
msg:"登录成功",
data:{
name:"admin",
age:18,
quxain: "admin"
}
})
}else{
ctx.body = JSON.stringify({
code: 200,
msg:"登录成功",
data:{
name:"user",
age:18,
quxain: "user"
}
})
}
}
module.exports = { module.exports = {
"GET /":index, "GET /":index,
"POST /addshop": addshop, "POST /addshop": addshop,
"GET /findshop": findshop "GET /findshop": findshop,
"POST /login": logins
} }