Compare commits
6 Commits
9156beda01
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 8765de91b3 | |||
| 3fc2d12c10 | |||
| d0868e6043 | |||
| 5fa9fd13f2 | |||
|
81585f81f7
|
|||
|
f458c472f7
|
13
app.js
13
app.js
@@ -1,9 +1,11 @@
|
||||
const Koa = require("koa");
|
||||
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)
|
||||
// import requter from "./bin/router"
|
||||
const app = new Koa();
|
||||
const bodyParser = require('koa-bodyparser');
|
||||
app.use(bodyParser());
|
||||
app.use(async (ctx, next) => {
|
||||
// 允许来自所有域名请求
|
||||
ctx.set("Access-Control-Allow-Origin", "*");
|
||||
@@ -40,9 +42,15 @@ app.use(async (ctx, next) => {
|
||||
// 需要获取其他字段时,使用Access-Control-Expose-Headers,
|
||||
// getResponseHeader('myData')可以返回我们所需的值
|
||||
//https://www.rails365.net/articles/cors-jin-jie-expose-headers-wu
|
||||
console.log(ctx.request.body)
|
||||
ctx.set("Access-Control-Expose-Headers", "myData");
|
||||
if(ctx.method == "OPTIONS"){
|
||||
ctx.body = ""
|
||||
|
||||
}else{
|
||||
await next();
|
||||
|
||||
}
|
||||
})
|
||||
app.use(async (ctx,next)=>{
|
||||
// await next();
|
||||
@@ -55,8 +63,7 @@ app.use(async (ctx,next)=>{
|
||||
});
|
||||
|
||||
|
||||
const bodyParser = require('koa-bodyparser');
|
||||
app.use(bodyParser());
|
||||
|
||||
// console.log(requter())
|
||||
app.use(requter());
|
||||
app.listen(3002);
|
||||
|
||||
@@ -6,11 +6,11 @@ function addMapping(router, mapping) {
|
||||
if (url.startsWith('GET ')) {
|
||||
var path = url.substring(4);
|
||||
router.get(path, mapping[url]);
|
||||
// console.log(`register URL mapping: GET ${path}`);
|
||||
console.log(`register URL mapping: GET ${path}`);
|
||||
} else if (url.startsWith('POST ')) {
|
||||
var path = url.substring(5);
|
||||
router.post(path, mapping[url]);
|
||||
// console.log(`register URL mapping: POST ${path}`);
|
||||
console.log(`register URL mapping: POST ${path}`);
|
||||
} else {
|
||||
console.log(`invalid URL: ${url}`);
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ let addshop = async (ctx,next)=>{
|
||||
// }
|
||||
// JSON.stringify(a)
|
||||
// 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){
|
||||
ctx.body = JSON.stringify({
|
||||
code: 1,
|
||||
@@ -38,46 +38,60 @@ let addshop = async (ctx,next)=>{
|
||||
})
|
||||
return ;
|
||||
}
|
||||
if(huodongjia == undefined){
|
||||
if(fenlei == undefined){
|
||||
ctx.body = JSON.stringify({
|
||||
code: 1,
|
||||
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: "商品详情不能为空"
|
||||
msg: "分类不能为空"
|
||||
})
|
||||
return ;
|
||||
}
|
||||
|
||||
|
||||
|
||||
ctx.body = await dbs.add("shop", ctx.request.body)
|
||||
// ctx.body="添加"
|
||||
|
||||
}
|
||||
|
||||
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 = {
|
||||
"GET /":index,
|
||||
"POST /addshop": addshop,
|
||||
"GET /findshop": findshop
|
||||
"GET /findshop": findshop,
|
||||
"POST /login": logins
|
||||
}
|
||||
Reference in New Issue
Block a user