In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to build a real-time warning system for severe weather with Node.JS". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "how to build a real-time warning system for severe weather with Node.JS" together!
Preamble:
These days, whether it's turning on TV or Short Video, it's all about Zhengzhou, Henan Province, encountering this unpredictable rainstorm, especially who can think of the subway station being flooded, trapped passengers situation is unimaginable. Hearing the post articles written by some witnesses, we listened to their escape at that time. We can imagine that they were helpless and helpless in the face of sudden disaster.
Zhengzhou High-tech Zone, where we are located, has also experienced water and power cuts. Until now, the radio and television bandwidth has not recovered the signal, and office colleagues are basically still connecting hot spots.
Step 1: Find the free weather forecast interface
There are many API interfaces for free weather access on the Internet. What I use here is the interface for aggregating data. The Dachang API is relatively stable.
Application Address: www.juhe.cn/docs/api/id/73
After the application is successful, a request key will be generated in the personal center, which will be used when sending the interface.
Step 2: Use the weather forecast interface and generate the program code
According to the usage instructions for aggregated data, we can debug the interface with the interface debugging tool, which we use here for API Post testing.
可以看到请求成功后的json格式如下:
{ "reason": "查询成功!", "result": { "city": "郑州", "realtime": { "temperature": "24", "humidity": "100", "info": "小雨", "wid": "07", "direct": "东北风", "power": "2级", "aqi": "32" }, "future": [ { "date": "2021-07-23", "temperature": "23/28℃", "weather": "小雨转阴", "wid": { "day": "07", "night": "02" }, "direct": "东风转北风" }, { "date": "2021-07-24", "temperature": "24/31℃", "weather": "小雨转多云", "wid": { "day": "07", "night": "01" }, "direct": "东北风转东风" }, { "date": "2021-07-25", "temperature": "23/31℃", "weather": "多云", "wid": { "day": "01", "night": "01" }, "direct": "东风转东南风" }, { "date": "2021-07-26", "temperature": "24/31℃", "weather": "小雨", "wid": { "day": "07", "night": "07" }, "direct": "东北风" }, { "date": "2021-07-27", "temperature": "23/31℃", "weather": "小雨转晴", "wid": { "day": "07", "night": "00" }, "direct": "东北风转南风" } ] }, "error_code": 0}
到这一步,我们已经拿到了未来7天的天气数据。
第三步:利用nodemailer在NodeJS中发送邮件
nodeJS的nodemailer用于发送邮件,很好用。通过以下命令安装即可使用:
npm install nodemailer
以下是我写的一个发送邮件的函数,其中邮箱账号和授权码可以在对应的邮箱服务商处获取。
/** * nodeJS 发送邮件 * * */function sendEmail(text){ let nodemailer = require('nodemailer'); let transporter = nodemailer.createTransport({ service:"126", // 邮箱 secure:true, // 安全的发送模式 auth:{ user:"be***er@126.com", // 发件人邮箱 pass:"MLQ***PYU"// 授权码,在邮件服务商处获取,126邮箱的获取地址为:https://help.mail.163.com/faq.do?m=list&categoryID=197 } }) let mailOptions = { from:"be***er@126.com", // 发件人邮箱,同上面的发件人邮箱保持一致就行 to:"3257132998@qq.com", // 收件人邮箱,也就是实时接收天气预报的邮箱 subject:"天气实时监控系统", // 邮件主题(标题) text:text // 邮件征文 } transporter.sendMail(mailOptions,(err,data) => { if(err){ console.log(err); res.json({status:400,msg:"send fail....."}) }else{ console.log(data); res.json({status:200,msg:"邮件发送成功....."}) } })} // 测试发邮件sendEmail('下雨了')
新建一个weather.js ,内容为以上代码,通过
node weather.js
即可测试邮件发送。
发送成功,邮件接收成功。
第四步:在nodeJS中定时获取天气并发送到指定邮箱
点击ApiPost右上角的生成NodeJS(Request)代码,即可生成在nodejs中请求聚合天气接口的程序代码,我们结合setInterval即可实现以上需求。
全部代码如下:
/** * nodeJS 发送邮件 * * */function sendEmail(text){ let nodemailer = require('nodemailer'); let transporter = nodemailer.createTransport({ service:"126", // 邮箱 secure:true, // 安全的发送模式 auth:{ user:"be***er@126.com", // 发件人邮箱 pass:"MLQ***PYU"// 授权码,在邮件服务商处获取,126邮箱的获取地址为:https://help.mail.163.com/faq.do?m=list&categoryID=197 } }) let mailOptions = { from:"be***er@126.com", // 发件人邮箱,同上面的发件人邮箱保持一致就行 to:"3257132998@qq.com", // 收件人邮箱,也就是实时接收天气预报的邮箱 subject:"天气实时监控系统", // 邮件主题(标题) text:text // 邮件征文 } transporter.sendMail(mailOptions,(err,data) => { if(err){ console.log(err); res.json({status:400,msg:"send fail....."}) }else{ console.log(data); res.json({status:200,msg:"邮件发送成功....."}) } })} setInterval(function(){ var request = require('request'); var headers = { 'User-Agent': 'Apipost client Runtime/+https://www.apipost.cn/' }; var options = { url: 'http://apis.juhe.cn/simpleWeather/query?city=%E9%83%91%E5%B7%9E&key=8763efe2a90b025c03e03fef95621cbc', headers: headers }; function callback(error, response, body) { let json = JSON.parse(body); console.log(json.result) if (!error && response.statusCode == 200) { sendEmail('郑州未来天气' + json.result.future[0].weather) } } request(options, callback); }, 300000);
至此,系统全部打造完毕。我们只需找个小服务器执行
node weather.js
命令即可每5分钟向指定邮箱发一次天气情况,当然,您也可以根据需要发送。
注意事项:
由于中文编码可能会在request中出现问题所以城市名称最好encode一下(右键)。
感谢各位的阅读,以上就是"如何用Node.JS打造一个恶劣天气实时预警系统"的内容了,经过本文的学习后,相信大家对如何用Node.JS打造一个恶劣天气实时预警系统这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.