Node Weather CLI



CLI - Command Line Interface - интерфейс командной строки.
CLI-приложение выводит данные в консоль.

Создадим приложение, которое выводит в консоль погоду указанного города

Устанавливаем модуль weather-js https://www.npmjs.com/package/weather-js
Подключаем его и используем

const weather = require('weather-js');

weather.find({search: 'San Francisco, CA', degreeType: 'C'}, function(errresult) {
  if(err) console.log(err);
  console.log(`There is ${result[0].current.temperature}°C in San Francisco.\nFeels like ${result[0].current.feelslike}°C, ${result[0].current.skytext.toLowerCase()}.\nTomorrow will be ${result[0].forecast[0].low} - ${result[0].forecast[0].high}.\nHave a nice day!`);
});

Определим город и страну пользователя.
Для этого устанавливаем и используем модуль readline-sync https://www.npmjs.com/package/readline-sync

const readlineSync = require('readline-sync');
const city = readlineSync.question("Please type in your city\n");
const country = readlineSync.question("Please type in your country\n");

Добавим немного фраз для вежливого диалога и приложение создано!

const weather = require('weather-js');
const readlineSync = require('readline-sync');

console.log("Hello! 。◕‿◕。\nWhat a lovely day! Let's check weather!\n");
const city = readlineSync.question("Please type in your city\n");
const country = readlineSync.question("Please type in your country\n");
const request = `${city}${country}`;

weather.find({search: request, degreeType: 'C'}, function(errresult) {
  if(err) console.log('Sorry, but you have not typed in your city name.');
 
  console.log(`There is ${result[0].current.temperature}°C in ${city}.\nFeels like ${result[0].current.feelslike}°C, ${result[0].current.skytext.toLowerCase()}.\nTomorrow will be ${result[0].forecast[0].low} - ${result[0].forecast[0].high}.\nHave a nice day!`);
});

Пример его работы

Код приложения https://github.com/irinainina/node.js/tree/node-weather