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

170
Vistas
Nodejs + Express always returning index.html

So, what is it, im not seeing here?

The problem is that no matter what i put as a file in:

app.get('/', function(req, res){
    res.sendfile(__dirname + "/index2");
});

It seem to always return index.html. As of now it says index2.html which indeed is a legit file with HTML page but whenever i run this server and go to localhost:8080 it will still serve the basic index.html not index2.html.

Also changing index.html will bring up the changes so its not cache problem i think.

But then again if you press the submit button, the app.post will work just fine and redirect you to kiitos.html

var express = require('express');
var bodyParser = require('body-parser');
var app = express();

app.use(bodyParser());

app.use(express.static(__dirname + '/'))


app.get('/', function(req, res){



res.sendfile(__dirname + "/index2.html");


});

app.post('/', function(req, res){

    var code = req.body.code;
    console.log(code);


  res.sendFile( __dirname + "/kiitos.html");

});



app.listen(8080);
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Your static folder is the one that serves the index.html file because its your root folder.

try creating a public folder for static files (like images, scripts and css files) and move your html files into a views folder and then use:

// save static files like images, scripts and css in `public`...
app.use(express.static(__dirname + '/public'))

app.get('/', function(req, res){

    // save html files in the `views` folder...
    res.sendfile(__dirname + "/views/index2.html");
});


app.post('/', function(req, res){

    var code = req.body.code;
    console.log(code);

    res.sendFile( __dirname + "/views/kiitos.html");
});
about 4 years ago · Santiago Trujillo Denunciar
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