Hi, I’m self learning Node.js recently and now i’m stuck.
I was building small weather app project using node.js and everytime i run res.write(readTimeData), localhost keeps loading infinitely. All the files (index.js, script.js and style.css) keeps pending.
Although i was getting right output of realTimeData in console but couldn’t get in browser.
I did found one solution, i.e. i have write all css and javascript in index.html file. But if i include external css and javascript then again it keeps loading. So, how can i get proper output with external files?
Any help is appreciated and please help me point out my mistakes.
Thank you!!!
Ig this will help you out
https://stackoverflow.com/a/3944751/3018595
const express = require(‘express’);
const https = require(‘https’);
const bodyParser = require(‘body-parser’);
const {parse} = require(‘path’);
const app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.get(‘/’, (req, res) => {
res.sendFile(__dirname + ‘/index.html’);
});
app.post(‘/’, (req, res) => {
const city = req.body.cityName;
const url = ‘https://api.openweathermap.org/data/2.5/weather?q=’ + city + ‘&appid=”API KEY”&units=metric’;
https.get(url, (response) => {
console.log(‘Code: ‘, response.statusCode);
response.on(‘data’, (data) => {
const weatherData = JSON.parse(data);
const temp = weatherData.main.temp;
const description = weatherData.weather[0].description;
const icon = weatherData.weather[0].icon;
const imgUrl = ‘<img src = http://openweathermap.org/img/wn/‘ + icon + ‘@2x.png>’;
res.write(‘<p>The whether is currently ‘ + description + ‘ <p>’);
res.write(‘<h1>The temperature in ‘ + city + ‘ is ‘ + temp + ‘ degree celcius</h1>’);
res.write(imgUrl);
res.send();
});
});
});
app.listen(3000, () => {
console.log(‘The server is running at port 3000’);
});