实战:npm天气预报工具

光学完没有,成果拿出来才有人信服!写一个小demo证明我学会了node基础~

package.json:

{
  "name": "node-weather12311",
  "version": "1.0.2",
  "description": "这是一简易的天气查询工具",
  "main": "index.js",
  "bin":{
    "weather":"./index.js"
    },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/HARRISKING/node-weather.git"
  },
  "keywords": [
    "weather"
  ],
  "author": "HARRISKING",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/HARRISKING/node-weather/issues"
  },
  "homepage": "https://github.com/HARRISKING/node-weather#readme",
  "dependencies": {
    "axios": "^0.16.1"
  }
}

index.js:

#!/usr/bin/env node

var axios = require('axios');
// console.log(process.argv);

var data = {};
if(process.argv[2]){
  data.params = {
    city: process.argv[2]
  }
}
axios.get('http://api.jirengu.com/weather.php',data)
.then(function(response){
  console.log('日期:'+response.data.results[0].weather_data[0].date)
  console.log('天气:'+response.data.results[0].weather_data[0].weather)
  console.log('风力:'+response.data.results[0].weather_data[0].wind)
  console.log('气温:'+response.data.results[0].weather_data[0].temperature)
  console.log('pm2.5:'+response.data.results[0].pm25)

}).catch(function(error){
  console.log('哎呀,出错了。。。')
})

结果:

这样,在下载之后,就可在通过输入“weather”查询此时当地的天气情况;
输入“weather + 地名”可以查询地名所在的天气状况。

成功!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,281评论 19 139
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,303评论 4 61
  • 他很火的时候我没有去看这本书但却一直挺想看。看的是电子书是因为闺蜜买了实体书,于是一是心痒开始看。没有想象中的惊艳...
    南方笑佳人阅读 434评论 0 0
  • 你有没有那种时候。 心里像是有心事,却又像是没事。 你很想找人说说,却发现不知该怎么说。 或是,你居然发现 ,你身...
    亓嵐阅读 181评论 0 0
  • 2017.1.2/3 解剖列车第3课☞知识点 1.腘绳肌无力导致骨盆前倾 2.骨盆是被动运动的,调整骨盆的2种策略...
    舒涵vivian阅读 792评论 3 6