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

135
Views
Soy nuevo en el nodo js, quiero enrutar una página desde un archivo html pero no funciona --- Problema de enrutamiento del nodo js

Soy nuevo en node js, tengo mi index.html y app.js

Mi índice.html

 <!DOCTYPE html> <html> <!-- \\ Bootstrap CDN --> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script> <body> <style> .input-group{ padding: 30px; } #button{ margin-left: 30px; } </style> <form action="/users" method="get" > <div class="input-group"> <span class="input-group-text">First and last name</span> <input type="text" name="name" class="form-control"> <input type="text" aria-label="Last_name" class="form-control"> <input type="submit" class="btn btn-primary" id="button"> </div> </form> </body> </html>

mi aplicación.js

 //For users page route const express = require('express') const app = express() app.get('/users',(req,res)=>{ res.send("data page") console.log("data page") }) // FOR THE HTML PAGE const http = require('http') const fs = require('fs') const server = http.createServer((req, res) => { res.writeHead(200, { 'content-type': 'text/html' }) fs.createReadStream('index.html').pipe(res) }) server.listen(process.env.PORT || 3000)

cuando ejecuto el archivo app.js, no está registrando la "página de datos" cuando hago clic en el botón Enviar de mi index.html, necesito ayuda, no estoy seguro de estar enrutándolo correctamente.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

No es necesario usar el módulo HTTP con Express y tampoco ha iniciado su aplicación Express.

si desea enviar un archivo HTML en el expreso (suponiendo que el archivo HTML esté en el mismo directorio).

 const express = require('express') const app = express() app.get('/',(req,res)=>{ res.sendFile('index.html',{root: __dirname }); }); app.get('/users',(req,res)=>{ res.send("data page") console.log("data page") }) app.listen(process.env.PORT || 3000);

Si quieres usar transmisiones, puedes seguir la respuesta de Rahul.

Si aún desea utilizar el módulo HTTP con express, puede leer esta publicación. ¿Por qué combinar el módulo http con el módulo express?

about 4 years ago · Juan Pablo Isaza Report

0

Cambie su código app.js a

 const fs = require('fs') const express = require('express') const app = express() app.get('/', (req, res) => { // for html page res.writeHead(200, { 'content-type': 'text/html' }) fs.createReadStream('index.html').pipe(res) }) app.get('/users', (req, res) => { res.send("data page") console.log("data page") }) app.listen(process.env.PORT || 3000)

En su archivo app.js actual, hay un problema de enrutamiento, ya que toda la ruta representará la página index.html

about 4 years ago · Juan Pablo Isaza Report
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!