日常总结 希望能帮到大家
1 mock/sever.js //创建服务
let http=require('http')
let fs=require('fs')
let url=require('url')
let siders=require('./sliders')
http.createServer((req,res)=>{
// 跨域请求头
res.setheader('Access-Control-Allow-Origin', '*');
res.setheader('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
res.setheader('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
if (req.method == 'OPTIONS') {
return res.end(200);
}
let {pathName,query}=url.parse(req.url,true). //true 把query 转换成对象
if(pathName==='./sliders'){
res.setHeader('Content-Type','application/json;charset=utf8')
res.end(Json.stringify(sliders))
}
}).listen(3000)
2 mock/sliders.js 模拟数据
module.exports=['url','url','url']
3 api/index.js
请求接口数据