繁体   English   中英

Nodejs + Express始终返回index.html

[英]Nodejs + Express always returning index.html

那是什么,我在这里没看到?

问题是,无论我将什么文件放入:

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

它似乎总是返回index.html 到目前为止,它说index2.html确实是带有HTML页面的合法文件,但是每当我运行此服务器并转到localhost:8080它仍将提供基本的index.html而不是index2.html

同样,更改index.html也会带来更改,因此我认为它不是缓存问题。

但是再一次,如果您按下提交按钮,则app.post将可以正常工作并将您重定向到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);

您的静态文件夹是提供index.html文件的文件夹,因为它是您的根文件夹。

尝试创建一个public静态文件的文件夹(如图片,脚本和CSS文件)和移动你的html文件到一个views文件夹,然后使用:

// 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");
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM