nodejs实战2:weather

本文介绍了一个使用Node.js构建的天气查询系统,涉及fs、request和qs模块。系统包括readIp、ip2geo、geo2weather、geo2weathers和writeFile五个组件,实现了从读取IP地址到获取并保存多个城市天气信息的流程。通过ip2geo获取城市地理位置,再利用geo2weather模块获取对应天气,最后将数据写入本地文件。
摘要由CSDN通过智能技术生成

说明:本文模块代码并非可直接调用,主要为函数调用的接口,需自行补充完整剩余部分(在各模块的注意部分有说明)

一、本项目使用的nodejs模块有:

  • fs文件模块
  • request模块
  • qs模块

二、需要构建的组件:

  1. readIp : 用于读取JSON格式的Ip地址;
  2. ip2geo:通过telize.com公共服务和解析的ip地址获取特定城市的信息;
  3. geo2weather:通过特定城市位置获取对应的天气情况;
  4. geo2weathers:通过ip2geo和geo2weather接口,并遍历解析的ip数组获取多个城市的weathers数据集合;
  5. writeWeather:将获取的weathers数据写入本地文件保存;

三、总体思路:

通过readip模块读取本地的JSON格式的IP地址数组,然后通过ip2geo模块获取相应城市位置相关信息的数组,并结合geo2weather模块一起供geo2weathers模块调用,遍历城市位置数组,以获取各城市的天气情况数组,最后通过writeIp模块保存下来。

四、各模块分析:

① readIp模块:

需查询的城市对应的IP地址:

[
    "115.29.230.208",
    "180.153.132.38",
    "75.125.235.224",
    "91.239.201.98",
    "60.28.215.115"
]

模块代码:

const fs = require('fs'); //引入fs模块
function readIp(path, callback){
   
	//path为ip路径,或者自己设置读取的相应位置
    fs.readFile(path, function(err, data){
   
        if(err){
   
            callback(err);
        }else{
   
            try{
   
                data = JSON.parse(data);
                callback(null, data)
            }catch(error){
   
                callback(error);
            }
        }
    })
}
module.exports.readIp = readIp
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The first edition of Node.js in Action was about web development with a particular focus on the Connect and Express web frameworks. Node.js in Action, Second Edition has been updated to suit the changing requirements of Node development. You’ll learn about front-end build systems, popular Node web frameworks, and how to build a web application with Express from scratch. You’ll also learn how to create automated tests and deploy Node web applications. Node is being increasingly used for command-line developer tools and desktop applications with Electron, so you’ll find chapters dedicated to both of these areas. This book assumes you’re familiar with basic programming concepts. The first chapter provides an overview of JavaScript and ES2015 for those of you who haven’t yet discovered the joys of modern JavaScript. Roadmap This book is organized into three parts. Part 1 provides an introduction to Node.js, teaching the fundamental techniques needed to develop with it. Chapter 1 explains the characteristics of JavaScript and Node and steps through example code. Chapter 2 guides you through fundamental Node.js programming concepts. Chapter 3 is a full tutorial on how to build a web application from scratch. Part 2, the largest section of the book, focuses on web application development. Chapter 4 dispels some of the mystery around front-end build systems: if you’ve ever had to use webpack or Gulp in a project but didn’t really understand it, this is the chapter for you. Chapter 5 reviews some of the most popular server-side frameworks available for Node, and chapter 6 goes into Connect and Express in more depth. Chapter 7 is dedicated to templating languages, which can improve your productivity when writing server-side code. Most web applications need a database, so chapter 8 covers the many types of databases that you can use with Node, from relational to NoSQL. Chapters 9 and 10 deal with testing and deployment, and this includes cloud deployment. Part 3 goes be
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值