Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

400
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda