针对第三方接口进行二次封装
扩展天气查询功能
weather.js:
导入模块:
const http = require('http');
导出查询天气封装好函数:
exports.queryWeather = (cityCode,callback) => {
let options = {
protocol : 'http:',
hostname : 'www.weather.com.cn',
port : 80,
path : '/data/sk/'+cityCode+'.html',
method : 'get'
}
let req = http.request(options,(res)=>{
let info = '';
res.on('data',(chunk)=>{
info += chunk;
});
res.on('end',()=>{
info = JSON.parse(info);
callback(info);
});
});
req.end();
}
07.js:
导入查询天气模块:
const weather = require('./weather.js');
加载模块:
weather.queryWeather('101020100',(data)=>{
console.log(data);
console.log(data.weatherinfo.WD);
});