最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Nodejs + Express always returning index.html - Stack Overflow

programmeradmin0浏览0评论

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

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);
Share Improve this question edited Apr 23, 2017 at 13:28 lch 2,1883 gold badges29 silver badges47 bronze badges asked Apr 23, 2017 at 9:37 user7085085user7085085
Add a ment  | 

1 Answer 1

Reset to default 7

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");
});
发布评论

评论列表(0)

  1. 暂无评论