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

188
Visualizações
GET routes not called in express with html

I have a very simple app so I didn't think I needed a full-fledged front-end template like angular, and I didn't really want to use Jade

app.get('*', function(req, res) {
  res.sendFile(__dirname + '/public/index.html');
});

Basically I'm returning an html file, and I'm able to call other html files like

login.html
signup.html

But what confuses me is that my routes are not called. For example I send a GET request to /login but there is no "LOGIN GET" output

//Edit: even with req, res, I can delete this function below and still receive index.html

app.get('/login',function(req,res){ 
  console.log("LOGIN GET")
  res.sendFile( path.join( __dirname, 'public', 'login.html' ));    
});

How is login.html sent to the client if my route isn't called?

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Missing req and res parameters to the route handler. For static file you can also follow https://expressjs.com/en/starter/static-files.html

app.get('/login',function(req, res){ 
  console.log("LOGIN GET")
  res.sendFile( path.join( __dirname, 'public', 'login.html' ));    
});
about 4 years ago · Santiago Trujillo Relatório

0

You should use the Express router.

var router = express.Router();

And then, you can get

router.get("/login",function(req,res){
  console.log("LOGIN GET")
  res.sendFile( path.join( __dirname, 'public', 'login.html' )); 
});
about 4 years ago · Santiago Trujillo Relatório

0

My guess is that you're declaring the catch-all route first:

app.get('*', function(req, res) {
  res.sendFile(__dirname + '/public/index.html');
});

app.get('/login',function(req,res){ 
  console.log("LOGIN GET")
  res.sendFile( path.join( __dirname, 'public', 'login.html' ));    
});

Because /login matches *, Express will use the first route to handle requests for /login as well. Generally, with Express you need to declare more specific routes first:

app.get('/login',function(req,res){ 
  console.log("LOGIN GET")
  res.sendFile( path.join( __dirname, 'public', 'login.html' ));    
});

app.get('*', function(req, res) {
  res.sendFile(__dirname + '/public/index.html');
});

The reason the other HTML files work is that you probably are using express.static() too (but not showing it).

about 4 years ago · Santiago Trujillo 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