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
4



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: |

Hui said the time period and nature of some offences “overlapped” and thus their prison terms could be served concurrently. The judge ordered Ng to be jailed for a total of six years and six months. Done! Now you’re the proud owner of a Telegram channel. The next step is to set up and customize your channel. Informative The Channel name and bio must be no more than 255 characters long ‘Ban’ on Telegram
from us


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