Compare commits

..

6 Commits

Author SHA1 Message Date
8765de91b3 删除字段 2021-09-08 14:46:03 +08:00
3fc2d12c10 添加了名称 2021-04-16 14:04:01 +08:00
d0868e6043 更新 'app.js' 2021-03-15 16:03:33 +08:00
5fa9fd13f2 更新 'app.js' 2021-03-15 16:00:55 +08:00
81585f81f7 Merge branch 'master' of http://git.luyuan.tk/luyuan/ceshikaoshi 2021-02-24 09:06:51 +08:00
f458c472f7 2 2021-02-24 09:06:18 +08:00
3 changed files with 53 additions and 32 deletions

15
app.js
View File

@@ -1,9 +1,11 @@
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();
const bodyParser = require('koa-bodyparser');
app.use(bodyParser());
app.use(async (ctx, next) => { app.use(async (ctx, next) => {
// 允许来自所有域名请求 // 允许来自所有域名请求
ctx.set("Access-Control-Allow-Origin", "*"); ctx.set("Access-Control-Allow-Origin", "*");
@@ -40,9 +42,15 @@ app.use(async (ctx, next) => {
// 需要获取其他字段时使用Access-Control-Expose-Headers // 需要获取其他字段时使用Access-Control-Expose-Headers
// 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
console.log(ctx.request.body)
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();
@@ -55,8 +63,7 @@ app.use(async (ctx,next)=>{
}); });
const bodyParser = require('koa-bodyparser');
app.use(bodyParser());
// console.log(requter()) // console.log(requter())
app.use(requter()); app.use(requter());
app.listen(3002); app.listen(3002);

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

@@ -16,7 +16,7 @@ let addshop = async (ctx,next)=>{
// } // }
// JSON.stringify(a) // JSON.stringify(a)
// console.log(ctx.request.body) // console.log(ctx.request.body)
let { mingcheng, fenlei, maidian, jiage, huodongjia, kucun, shangjia ,xiangqing } = ctx.request.body let { mingcheng, fenlei, maidian, jiage} = ctx.request.body
if(mingcheng == undefined){ if(mingcheng == undefined){
ctx.body = JSON.stringify({ ctx.body = JSON.stringify({
code: 1, code: 1,
@@ -38,34 +38,15 @@ let addshop = async (ctx,next)=>{
}) })
return ; return ;
} }
if(huodongjia == undefined){ if(fenlei == undefined){
ctx.body = JSON.stringify({ ctx.body = JSON.stringify({
code: 1, code: 1,
msg: "活动价格不能为空" msg: "分类不能为空"
})
return ;
}
if(kucun == undefined){
ctx.body = JSON.stringify({
code: 1,
msg: "库存不能为空"
})
return ;
}
if(shangjia == undefined){
ctx.body = JSON.stringify({
code: 1,
msg: "是否上架不能为空"
})
return ;
}
if(xiangqing == undefined){
ctx.body = JSON.stringify({
code: 1,
msg: "商品详情不能为空"
}) })
return ; return ;
} }
ctx.body = await dbs.add("shop", ctx.request.body) ctx.body = await dbs.add("shop", ctx.request.body)
// ctx.body="添加" // ctx.body="添加"
@@ -73,11 +54,44 @@ let addshop = async (ctx,next)=>{
} }
let findshop = async (ctx, next) => { let findshop = async (ctx, next) => {
ctx.body = await dbs.find("shop") let q = {}
if(ctx.query.name){
q.mingcheng = ctx.query.name
}
ctx.body = await dbs.find("shop",q)
}
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
} }