Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

408
Visualizações
How to use node.js from one html file to go to another html file and operate on variables from app.js including 2 html files

in index.html I have a form which, after typing the city name, sends information about the place and displays it:

 res.write("<p>The weather is currently " + desc + "</p>");
 res.write("<h1>The temperature in " + query + " is " + temp + " degrees Celcuis.</h1>");
 res.write("<img src=" + imageURL + ">");

and I would like it to be displayed in weather.html

my all code from app.js:

const express = require("express");
const https = require("https");
const bodyParser = require("body-parser");
const app = express();
const path = require('path'); //to CSS work

app.use(bodyParser.urlencoded({ extended: true })); //using body parser
app.use(express.static(path.join(__dirname, 'public'))); //to CSS work

app.get("/", (req, res) => {
    res.sendFile(__dirname + "/index.html");
});

app.post("/", (req, res) => {

    const query = req.body.City;
    const keyAPI = "mykey";
    const units = "metric";
    const url = "https://api.openweathermap.org/data/2.5/weather?q=" + query + "&appid=" + keyAPI + "&units=" + units + "";

    https.get(url, (response) => {
        console.log(response.statusCode);

        response.on("data", (data) => {
            const weatherData = JSON.parse(data);
            const temp = weatherData.main.temp;
            const desc = weatherData.weather[0].description;
            const imageIcon = weatherData.weather[0].icon;
            const imageURL = "http://openweathermap.org/img/wn/" + imageIcon + "@2x.png";


            //res.sendFile(__dirname + "/weather.html");  <-- It doesn't work'
            res.write("<p>The weather is currently " + desc + "</p>");
            res.write("<h1>The temperature in " + query + " is " + temp + " degrees Celcuis.</h1>");
            res.write("<img src=" + imageURL + ">");
            res.send();
        });

    });
});

app.listen(3000, () => {
    console.log("Server is running on port 3000.");
});
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda