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

137
Visualizações
I am new to node js , I want to route a page from html file but its not working--- Node js routing problem

I am new to node js, I have my index.html and app.js

My Index.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>

my app.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)

when I run the app.js file, it's not logging "data page" when I click submit button from my index.html, need help, am not sure am routing it properly.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

It is not necessary to use the HTTP module with express also you have not started your express app.

if you want to send an HTML file in the express (assuming the HTML file is in the same directory).

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);

If you want to use streams you can follow Rahul's Answer

If you still want to use the HTTP module with express you can read this post. Why combine http module with express module

about 4 years ago · Juan Pablo Isaza Relatório

0

Change your app.js code to

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)


In your current app.js file there is a routing issue as all the route will render the index.html page

about 4 years ago · Juan Pablo Isaza Relatório
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