0% found this document useful (0 votes)
35 views4 pages

Lab 13 Web Eng

The lab report describes a Node.js script that connects to the Open Weather Map API to retrieve current weather data for a specified city using the request and querystring modules. It also shows how to parse the JSON response to extract relevant weather data like temperature, humidity, and wind speed. The report demonstrates using an event emitter to send the weather data to a separate module for additional processing or formatting of the data.

Uploaded by

Muhammad Qasim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views4 pages

Lab 13 Web Eng

The lab report describes a Node.js script that connects to the Open Weather Map API to retrieve current weather data for a specified city using the request and querystring modules. It also shows how to parse the JSON response to extract relevant weather data like temperature, humidity, and wind speed. The report demonstrates using an event emitter to send the weather data to a separate module for additional processing or formatting of the data.

Uploaded by

Muhammad Qasim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab Report 13

Department of Computer Science

HITEC University, Taxila

Web Engineering

Submitted by:

Haseeb Ahmed Ansari (19-cs-026)

Submitted To: Sir Adnan Javed


1. Write a Node.js script that connects to the Open Weather Map API and retrieves the current

weather data for a specified city. Use the https module to make the API call, and the querystring

module to construct the API:

const request = require('request');

var API_KEY = 'your_api_key';

const forecast = function (latitude, longitude) {

var url = `http://api.openweathermap.org/data/2.5/weather?`

+`lat=${latitude}&lon=${longitude}&appid=${API_KEY}`

request({ url: url, json: true }, function (error, response) {

if (error) {

console.log('Unable to connect to Forecast API');

else {

console.log('It is currently '

+ response.body.main.temp

+ ' degrees out.'

);

console.log('The high today is '

+ response.body.main.temp_max

+ ' with a low of '

+ response.body.main.temp_min

);
console.log('Humidity today is '

+ response.body.main.humidity

);

})

var latitude = 22.7196; // Indore latitude

var longitude = 75.8577; // Indore longitude

// Function call

forecast(latitude, longitude);

npm install request


npm version request
node index.js

b. Parse the JSON response from the API call and extract the relevant
weather data, such as temperature, humidity, and wind speed.
def get_weather_data(location):
    # Set the parameters
    params = {
        'location': location,
        'fields': 'temperature,humidity,wind_speed',
        'units': 'metric',
        'timesteps': '1h',
        'apikey': api_key
    }
 
    # Make the API request
    response = requests.get(url, params=params)
 
    # Parse the response
    weather_data = response.json()
    temperature = weather_data['data']['timelines'][0]['intervals'][0]['values']['temperature']
    humidity = weather_data['data']['timelines'][0]['intervals'][0]['values']['humidity']
    wind_speed = weather_data['data']['timelines'][0]['intervals'][0]['values']['wind_speed']
 
    # Return the weather data
    return {'temperature': temperature, 'humidity': humidity, 'wind_speed': wind_speed}

c. Use the Event module in Node.js to create an event emitter that


sends the weather data to a separate module for processing.
const myEmitter = new EventEmitter();

function c1() {
console.log('an event occurred!');
}

function c2() {
console.log('yet another event occurred!');
}

myEmitter.on('eventOne', c1); // Register for eventOne


myEmitter.on('eventOne', c2); // Register for eventOne

4. In the processing module, create an event listener that receives the weather data and
performs additional calculations or formatting as needed. For example, you could convert the
temperature from Celsius to Fahrenheit, or display the weather data in a user-friendly
format.

function temperatureConverter(valNum) {
  valNum = parseFloat(valNum);
  document.getElementById("outputCelsius").innerHTML = (valNum-32) / 1.8;
}

You might also like