美文网首页
实战:npm天气预报工具

实战:npm天气预报工具

作者: 春木橙云 | 来源:发表于2017-06-02 12:16 被阅读35次

    光学完没有,成果拿出来才有人信服!写一个小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 + 地名”可以查询地名所在的天气状况。

    成功!

    相关文章

      网友评论

          本文标题:实战:npm天气预报工具

          本文链接:https://www.haomeiwen.com/subject/zoxufxtx.html