WE_USE_JS Telegram 4959
👩‍💻 Задачка по NodeJS

Напишите скрипт, который принимает название города и выводит текущую температуру в этом городе, используя бесплатный API OpenWeatherMap.

Пример использования:

node weather.js London
Текущая температура в London: 15°C


Решение задачи ⬇️

const https = require('https');

const city = process.argv[2];
const apiKey = 'your_openweathermap_api_key'; // Замените на свой API-ключ

if (!city) {
console.error('Пожалуйста, укажите город.');
process.exit(1);
}

const url = `
https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${apiKey}`;

https.get(url, (res) => {
let data = '';

res.on('data', (chunk) => {
data += chunk;
});

res.on('end', () => {
const weather = JSON.parse(data);
if (weather.main) {
console.log(`Текущая температура в ${city}: ${weather.main.temp}°C`);
} else {
console.error(`Не удалось получить данные о погоде для города: ${city}`);
}
});
}).on('error', (err) => {
console.error('Ошибка:', err.message);
});
Please open Telegram to view this post
VIEW IN TELEGRAM
3



tgoop.com/we_use_js/4959
Create:
Last Update:

👩‍💻 Задачка по NodeJS

Напишите скрипт, который принимает название города и выводит текущую температуру в этом городе, используя бесплатный API OpenWeatherMap.

Пример использования:

node weather.js London
Текущая температура в London: 15°C


Решение задачи ⬇️

const https = require('https');

const city = process.argv[2];
const apiKey = 'your_openweathermap_api_key'; // Замените на свой API-ключ

if (!city) {
console.error('Пожалуйста, укажите город.');
process.exit(1);
}

const url = `
https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${apiKey}`;

https.get(url, (res) => {
let data = '';

res.on('data', (chunk) => {
data += chunk;
});

res.on('end', () => {
const weather = JSON.parse(data);
if (weather.main) {
console.log(`Текущая температура в ${city}: ${weather.main.temp}°C`);
} else {
console.error(`Не удалось получить данные о погоде для города: ${city}`);
}
});
}).on('error', (err) => {
console.error('Ошибка:', err.message);
});

BY Node.JS [ru] | Серверный JavaScript


Share with your friend now:
tgoop.com/we_use_js/4959

View MORE
Open in Telegram


Telegram News

Date: |

bank east asia october 20 kowloon Telegram Channels requirements & features How to create a business channel on Telegram? (Tutorial) Invite up to 200 users from your contacts to join your channel The imprisonment came as Telegram said it was "surprised" by claims that privacy commissioner Ada Chung Lai-ling is seeking to block the messaging app due to doxxing content targeting police and politicians.
from us


Telegram Node.JS [ru] | Серверный JavaScript
FROM American