Nodejs 分享给大家一个好用的天气API
Nodejs 分享给大家一个好用的天气API
使用jsonp方式的好处是javascript可以跨域访问jsonp服务,只要客户端能连接外网就能访问到,不需要服务器端处理,没有服务器压力。
使用方式,直接访问地址,将得到js,将下面的地址引入到script标签
//指定天气信息给变量方式
http://9snow.org/weather/api?city=北京&var=test1
//指定callback函数方式
http://9snow.org/weather/api?city=北京&callback=test2
只带city参数直接返回城市天气的json数据
展示如下:
指定变量方式:
var test=天气数据
指定callback方式:
test2(天气数据)
数据格式如下:
{“weatherinfo”:{“city”:“北京”,“city_en”:“beijing”,“date_y”:“2010年10月30日”,“date”:“庚寅年九月廿三”,“week”:“星期六”,“fchh”:“08”,“cityid”:“101010100”,“temp1”:“18℃~5℃”,“temp2”:“19℃~6℃”,“temp3”:“14℃~3℃”,“temp4”:“14℃~3℃”,“temp5”:“15℃~5℃”,“temp6”:“15℃~5℃”,“tempF1”:“64.4℉~41℉”,“tempF2”:“66.2℉~42.8℉”,“tempF3”:“57.2℉~37.4℉”,“tempF4”:“57.2℉~37.4℉”,“tempF5”:“59℉~41℉”,“tempF6”:“59℉~41℉”,“weather1”:“晴”,“weather2”:“晴转多云”,“weather3”:“多云转晴”,“weather4”:“晴转多云”,“weather5”:“多云转晴”,“weather6”:“晴”,“img1”:“0”,“img2”:“99”,“img3”:“0”,“img4”:“1”,“img5”:“1”,“img6”:“0”,“img7”:“0”,“img8”:“1”,“img9”:“1”,“img10”:“0”,“img11”:“0”,“img12”:“99”,“img_single”:“0”,“img_title1”:“晴”,“img_title2”:“晴”,“img_title3”:“晴”,“img_title4”:“多云”,“img_title5”:“多云”,“img_title6”:“晴”,“img_title7”:“晴”,“img_title8”:“多云”,“img_title9”:“多云”,“img_title10”:“晴”,“img_title11”:“晴”,“img_title12”:“晴”,“img_title_single”:“晴”,“wind1”:“微风”,“wind2”:“微风”,“wind3”:“微风”,“wind4”:“微风”,“wind5”:“微风”,“wind6”:“微风”,“fx1”:“微风”,“fx2”:“微风”,“fl1”:“小于3级”,“fl2”:“小于3级”,“fl3”:“小于3级”,“fl4”:“小于3级”,“fl5”:“小于3级”,“fl6”:“小于3级”,“index”:“温凉”,“index_d”:“较凉爽,建议着夹衣加薄羊毛衫等春秋服装。体弱者宜着夹衣加羊毛衫。因昼夜温差较大,注意增减衣服。”,“index48”:“舒适”,“index48_d”:“建议着薄型套装等春秋过渡装。年老体弱者宜着套装。但昼夜温差较大,注意适当增减衣服。”,“index_uv”:“中等”,“index48_uv”:“中等”,“index_xc”:“适宜”,“index_tr”:“很适宜”,“index_co”:“舒适”,“st1”:“19”,“st2”:“7”,“st3”:“18”,“st4”:“7”,“st5”:“13”,“st6”:“5”,“index_cl”:“适宜”,“index_ls”:“适宜”}}
获取到这些数据以后,客户端做处理既可,这种方式可以大大的减少nodejs服务器端的压力,而且可以做成移动的客户端哦,希望对大家有作用,如果有问题,大家可以在下面留言
Nodejs 分享给大家一个好用的天气API
在现代Web开发中,使用外部API来获取实时数据是一个常见的需求。本文将介绍如何使用JSONP(JSON with Padding)方式通过Node.js来获取天气信息,并提供一些示例代码。
使用JSONP的好处
JSONP的主要优点是它允许JavaScript跨域访问数据。这意味着你只需要确保客户端能够连接到互联网,就可以直接访问该API,而无需服务器端进行额外处理,从而大大减少了服务器的压力。
如何使用这个天气API
你可以通过以下两种方式来获取天气信息:
-
指定变量方式:
- 访问指定的URL并传入
city
和var
参数。 - 将返回的数据赋值给指定的变量名。
- 访问指定的URL并传入
-
指定callback函数方式:
- 访问指定的URL并传入
city
和callback
参数。 - 将返回的数据作为回调函数的参数传递。
- 访问指定的URL并传入
示例代码
假设你想获取北京的天气信息,可以通过以下方式来实现:
HTML部分
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Weather API Example</title>
</head>
<body>
<div id="weather"></div>
<script src="https://9snow.org/weather/api?city=北京&var=data"></script>
<script>
// 指定变量方式
console.log(data.weatherinfo);
// 或者使用指定callback方式
window.onload = function() {
var script = document.createElement('script');
script.src = 'https://9snow.org/weather/api?city=北京&callback=handleWeatherData';
document.body.appendChild(script);
};
function handleWeatherData(weather) {
console.log(weather.weatherinfo);
}
</script>
</body>
</html>
解释
-
指定变量方式:
- 在HTML中引入了
<script>
标签,并传入city
和var
参数。 - 返回的数据将赋值给全局变量
data
。 - 你可以通过
console.log(data.weatherinfo)
来查看天气信息。
- 在HTML中引入了
-
指定callback函数方式:
- 动态创建
<script>
标签,并传入city
和callback
参数。 - 返回的数据将作为回调函数
handleWeatherData
的参数。 - 你可以通过
console.log(weather.weatherinfo)
来查看天气信息。
- 动态创建
数据格式
获取到的天气数据格式如下:
{
"weatherinfo": {
"city": "北京",
"city_en": "beijing",
"date_y": "2010年10月30日",
"date": "庚寅年九月廿三",
"week": "星期六",
"fchh": "08",
"cityid": "101010100",
"temp1": "18℃~5℃",
"temp2": "19℃~6℃",
"temp3": "14℃~3℃",
"temp4": "14℃~3℃",
"temp5": "15℃~5℃",
"temp6": "15℃~5℃",
"tempF1": "64.4℉~41℉",
"tempF2": "66.2℉~42.8℉",
"tempF3": "57.2℉~37.4℉",
"tempF4": "57.2℉~37.4℉",
"tempF5": "59℉~41℉",
"tempF6": "59℉~41℉",
"weather1": "晴",
"weather2": "晴转多云",
"weather3": "多云转晴",
"weather4": "晴转多云",
"weather5": "多云转晴",
"weather6": "晴",
"img1": "0",
"img2": "99",
"img3": "0",
"img4": "1",
"img5": "1",
"img6": "0",
"img7": "0",
"img8": "1",
"img9": "1",
"img10": "0",
"img11": "0",
"img12": "99",
"img_single": "0",
"img_title1": "晴",
"img_title2": "晴",
"img_title3": "晴",
"img_title4": "多云",
"img_title5": "多云",
"img_title6": "晴",
"img_title7": "晴",
"img_title8": "多云",
"img_title9": "多云",
"img_title10": "晴",
"img_title11": "晴",
"img_title12": "晴",
"img_title_single": "晴",
"wind1": "微风",
"wind2": "微风",
"wind3": "微风",
"wind4": "微风",
"wind5": "微风",
"wind6": "微风",
"fx1": "微风",
"fx2": "微风",
"fl1": "小于3级",
"fl2": "小于3级",
"fl3": "小于3级",
"fl4": "小于3级",
"fl5": "小于3级",
"fl6": "小于3级",
"index": "温凉",
"index_d": "较凉爽,建议着夹衣加薄羊毛衫等春秋服装。体弱者宜着夹衣加羊毛衫。因昼夜温差较大,注意增减衣服。",
"index48": "舒适",
"index48_d": "建议着薄型套装等春秋过渡装。年老体弱者宜着套装。但昼夜温差较大,注意适当增减衣服。",
"index_uv": "中等",
"index48_uv": "中等",
"index_xc": "适宜",
"index_tr": "很适宜",
"index_co": "舒适",
"st1": "19",
"st2": "7",
"st3": "18",
"st4": "7",
"st5": "13",
"st6": "5",
"index_cl": "适宜",
"index_ls": "适宜"
}
}
总结
通过以上方法,你可以轻松地在Node.js项目中集成这个天气API,并根据需要显示天气信息。这种方式不仅简单易用,而且大大减轻了服务器端的压力。希望这些示例对你有所帮助!如果有任何问题或建议,请在下方留言讨论。
我在用phonegap 开发的时候跨域调用一点问题都木有,希望对你有用
嗯,不错,收藏了
这个资源是中国天气网的资源的jsonp方式,特别适合ajax跨域调用,希望对你有用
Node.js 分享给大家一个好用的天气API
在这个帖子中,我将介绍如何利用一个简单且高效的天气API来获取并显示天气信息。这个API支持JSONP方式,能够帮助我们跨域获取数据,减轻服务器端的压力。
使用方式
这个天气API的使用非常简单,只需向特定的URL发送请求,并传入城市名称即可。该API会以JSON或JSONP的方式返回数据。
示例链接:
-
指定变量方式:
<a href="http://9snow.org/weather/api?city=%E5%8C%97%E4%BA%AC&var=test1">http://9snow.org/weather/api?city=北京&var=test1</a>
-
指定callback函数方式:
<a href="http://9snow.org/weather/api?city=%E5%8C%97%E4%BA%AC&callback=test2">http://9snow.org/weather/api?city=北京&callback=test2</a>
数据格式
API返回的数据格式如下:
{
"weatherinfo": {
"city": "北京",
"city_en": "beijing",
"date_y": "2010年10月30日",
"date": "庚寅年九月廿三",
"week": "星期六",
"fchh": "08",
"cityid": "101010100",
"temp1": "18℃~5℃",
"temp2": "19℃~6℃",
"temp3": "14℃~3℃",
"temp4": "14℃~3℃",
"temp5": "15℃~5℃",
"temp6": "15℃~5℃",
"tempF1": "64.4℉~41℉",
"tempF2": "66.2℉~42.8℉",
"tempF3": "57.2℉~37.4℉",
"tempF4": "57.2℉~37.4℉",
"tempF5": "59℉~41℉",
"tempF6": "59℉~41℉",
"weather1": "晴",
"weather2": "晴转多云",
"weather3": "多云转晴",
"weather4": "晴转多云",
"weather5": "多云转晴",
"weather6": "晴",
"img1": "0",
"img2": "99",
"img3": "0",
"img4": "1",
"img5": "1",
"img6": "0",
"img7": "0",
"img8": "1",
"img9": "1",
"img10": "0",
"img11": "0",
"img12": "99",
"img_single": "0",
"img_title1": "晴",
"img_title2": "晴",
"img_title3": "晴",
"img_title4": "多云",
"img_title5": "多云",
"img_title6": "晴",
"img_title7": "晴",
"img_title8": "多云",
"img_title9": "多云",
"img_title10": "晴",
"img_title11": "晴",
"img_title12": "晴",
"img_title_single": "晴",
"wind1": "微风",
"wind2": "微风",
"wind3": "微风",
"wind4": "微风",
"wind5": "微风",
"wind6": "微风",
"fx1": "微风",
"fx2": "微风",
"fl1": "小于3级",
"fl2": "小于3级",
"fl3": "小于3级",
"fl4": "小于3级",
"fl5": "小于3级",
"fl6": "小于3级",
"index": "温凉",
"index_d": "较凉爽,建议着夹衣加薄羊毛衫等春秋服装。体弱者宜着夹衣加羊毛衫。因昼夜温差较大,注意增减衣服。",
"index48": "舒适",
"index48_d": "建议着薄型套装等春秋过渡装。年老体弱者宜着套装。但昼夜温差较大,注意适当增减衣服。",
"index_uv": "中等",
"index48_uv": "中等",
"index_xc": "适宜",
"index_tr": "很适宜",
"index_co": "舒适",
"st1": "19",
"st2": "7",
"st3": "18",
"st4": "7",
"st5": "13",
"st6": "5",
"index_cl": "适宜",
"index_ls": "适宜"
}
}
示例代码
为了方便大家理解如何使用这个API,以下是一个简单的Node.js示例代码,展示如何通过request
库获取数据,并将其解析为JSON对象:
const request = require('request');
function getWeather(city) {
const url = `http://9snow.org/weather/api?city=${encodeURIComponent(city)}&callback=test`;
request({ url, json: true }, (err, res, data) => {
if (err) {
console.error(err);
return;
}
const weatherInfo = data.weatherinfo;
console.log(weatherInfo);
});
}
getWeather('北京');
这段代码首先定义了一个函数getWeather
,接收城市名称作为参数。然后,它构建了请求URL,并使用request
库发起GET请求。成功获取数据后,将数据中的weatherinfo
部分打印出来。
总结
这个天气API提供了丰富的天气信息,包括温度、天气状况、风力等,非常适合用于个人项目或移动应用。由于其JSONP支持,我们可以轻松地在浏览器中跨域调用该API,而无需额外处理服务器端逻辑。希望这个分享能对你有所帮助!