Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

332
Views
No se puede analizar la variable javascript del archivo local usando jsdom & express

Estoy tratando de usar una variable de JavaScript que se representa en el cliente en mi secuencia de comandos de Node, pero Node sigue diciendo que la variable no está definida. Aquí está el código:

prueba.js

 const express = require('express'); const jsdom = require("jsdom"); const { JSDOM } = jsdom; // it exports a JSDOM class var app = express(); app.get('/', function(req, res) { res.sendFile('url_grabber.html', {root: __dirname }) }); app.listen(3000); //port // Create "your" server const local = express() .use('/', (req, res) => { JSDOM.fromURL('http://localhost:3000/', { runScripts: "dangerously", resources: "usable" // allow <script> to run }).then((dom) => { // pass back the result of "target_url" from the context of the // loaded dom page. res.send(dom.window.target_url); console.log(url_string); }); }) .listen(3001);

url_grabber.html

 <!DOCTYPE html> <html> <body> <p>Enter your URL to download: <input type="text" id="url_string" value=""></p> <button onclick="saveUrl()">Click Me!</button> <script> function saveUrl() { var target_url = document.getElementById("url_string").value console.log(target_url) document.getElementById('url_string').value = '' //clears text box } </script> </body> </html>

La secuencia de comandos del nodo falla cuando cargo la página en el puerto 3001. Realmente no quiero que se sirva nada aparte de mi HTML, pero eso es lo que sugiere el ejemplo que encontré en SO. Tenga en cuenta que si en línea HTML básico, entonces el código funciona, como se describe aquí: node.js analizando texto html para obtener un valor para una variable javascript

Ese ejemplo funciona, pero mi HTML es demasiado grande para estar en línea de manera efectiva (¿a menos que use JSDOM serialize tal vez?)

Preferiría mantener el HTML/JS en un archivo separado para facilitar la lectura si es posible.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!