完成
This commit is contained in:
		
							parent
							
								
									bb3f3ec336
								
							
						
					
					
						commit
						2ef9823b2f
					
				
							
								
								
									
										40
									
								
								app.js
									
									
									
									
									
								
							
							
						
						
									
										40
									
								
								app.js
									
									
									
									
									
								
							| @ -4,6 +4,46 @@ dbs = require("./bin/mongodb.js")('mongodb://localhost:27017/ceshikaoshi',"ceshi | |||||||
| // console.log(requter)
 | // console.log(requter)
 | ||||||
| // import requter from "./bin/router"
 | // import requter from "./bin/router"
 | ||||||
| const app = new Koa(); | const app = new Koa(); | ||||||
|  | app.use(async (ctx, next) => { | ||||||
|  |     // 允许来自所有域名请求
 | ||||||
|  |     ctx.set("Access-Control-Allow-Origin", "*"); | ||||||
|  |     // 这样就能只允许 http://localhost:8080 这个域名的请求了
 | ||||||
|  |     // ctx.set("Access-Control-Allow-Origin", "http://localhost:8080"); 
 | ||||||
|  |     // 设置所允许的HTTP请求方法
 | ||||||
|  |     ctx.set("Access-Control-Allow-Methods", "OPTIONS, GET, PUT, POST, DELETE"); | ||||||
|  | 
 | ||||||
|  |     // 字段是必需的。它也是一个逗号分隔的字符串,表明服务器支持的所有头信息字段.
 | ||||||
|  |     ctx.set("Access-Control-Allow-Headers", "x-requested-with, accept, origin, content-type"); | ||||||
|  | 
 | ||||||
|  |     // 服务器收到请求以后,检查了Origin、Access-Control-Request-Method和Access-Control-Request-Headers字段以后,确认允许跨源请求,就可以做出回应。
 | ||||||
|  |     // Content-Type表示具体请求中的媒体类型信息
 | ||||||
|  |     ctx.set("Content-Type", "application/json;charset=utf-8"); | ||||||
|  | 
 | ||||||
|  |     // 该字段可选。它的值是一个布尔值,表示是否允许发送Cookie。默认情况下,Cookie不包括在CORS请求之中。
 | ||||||
|  |     // 当设置成允许请求携带cookie时,需要保证"Access-Control-Allow-Origin"是服务器有的域名,而不能是"*";
 | ||||||
|  |     ctx.set("Access-Control-Allow-Credentials", true); | ||||||
|  | 
 | ||||||
|  |     // 该字段可选,用来指定本次预检请求的有效期,单位为秒。
 | ||||||
|  |     // 当请求方法是PUT或DELETE等特殊方法或者Content-Type字段的类型是application/json时,服务器会提前发送一次请求进行验证
 | ||||||
|  |     // 下面的的设置只本次验证的有效时间,即在该时间段内服务端可以不用进行验证
 | ||||||
|  |     ctx.set("Access-Control-Max-Age", 300); | ||||||
|  | 
 | ||||||
|  |     /* | ||||||
|  |     CORS请求时,XMLHttpRequest对象的getResponseHeader()方法只能拿到6个基本字段: | ||||||
|  |         Cache-Control、 | ||||||
|  |         Content-Language、 | ||||||
|  |         Content-Type、 | ||||||
|  |         Expires、 | ||||||
|  |         Last-Modified、 | ||||||
|  |         Pragma。 | ||||||
|  |     */ | ||||||
|  |     // 需要获取其他字段时,使用Access-Control-Expose-Headers,
 | ||||||
|  |     // getResponseHeader('myData')可以返回我们所需的值
 | ||||||
|  |     //https://www.rails365.net/articles/cors-jin-jie-expose-headers-wu
 | ||||||
|  |     ctx.set("Access-Control-Expose-Headers", "myData"); | ||||||
|  |      | ||||||
|  |     await next(); | ||||||
|  | }) | ||||||
| app.use(async (ctx,next)=>{ | app.use(async (ctx,next)=>{ | ||||||
|     // await next();
 |     // await next();
 | ||||||
|     console.log(ctx.url) |     console.log(ctx.url) | ||||||
|  | |||||||
| @ -1,7 +1,83 @@ | |||||||
| let index = async (ctx,next)=>{ | let index = async (ctx,next)=>{ | ||||||
|     ctx.body = "" |     ctx.body = "欢迎" | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | let addshop = async (ctx,next)=>{ | ||||||
|  |     // a = {
 | ||||||
|  |     //     mingcheng:"mingzi",
 | ||||||
|  |     //     fenlei: 1,
 | ||||||
|  |     //     maidian:"lalala",
 | ||||||
|  |     //     jiage:12,
 | ||||||
|  |     //     huodongjia:10,
 | ||||||
|  |     //     kucun:100,
 | ||||||
|  |     //     xiaoliang:50,
 | ||||||
|  |     //     shangjia:1,
 | ||||||
|  |     //     xiangqing:"xiangqing",
 | ||||||
|  |     // }
 | ||||||
|  |     // JSON.stringify(a)
 | ||||||
|  |     // console.log(ctx.request.body)
 | ||||||
|  |     let { mingcheng, fenlei, maidian, jiage, huodongjia, kucun, shangjia ,xiangqing } = ctx.request.body | ||||||
|  |     if(mingcheng == undefined){ | ||||||
|  |         ctx.body = JSON.stringify({ | ||||||
|  |             code: 1, | ||||||
|  |             msg: "名称不能为空" | ||||||
|  |         }) | ||||||
|  |         return ; | ||||||
|  |     } | ||||||
|  |     if(maidian == undefined){ | ||||||
|  |         ctx.body = JSON.stringify({ | ||||||
|  |             code: 1, | ||||||
|  |             msg: "卖点不能为空" | ||||||
|  |         }) | ||||||
|  |         return ; | ||||||
|  |     } | ||||||
|  |     if(jiage == undefined){ | ||||||
|  |         ctx.body = JSON.stringify({ | ||||||
|  |             code: 1, | ||||||
|  |             msg: "价格不能为空" | ||||||
|  |         }) | ||||||
|  |         return ; | ||||||
|  |     } | ||||||
|  |     if(huodongjia == 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: "商品详情不能为空" | ||||||
|  |         }) | ||||||
|  |         return ; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     ctx.body = await dbs.add("shop", ctx.request.body) | ||||||
|  |     // ctx.body="添加"
 | ||||||
|  |      | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | let findshop = async (ctx, next) => { | ||||||
|  |     ctx.body = await dbs.find("shop") | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| module.exports = { | module.exports = { | ||||||
|     "GET /":index |     "GET /":index, | ||||||
|  |     "POST /addshop": addshop, | ||||||
|  |     "GET /findshop": findshop | ||||||
| } | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user