Javascript 如何从express\nodeJS中的post请求中获取数据,并在提交后将用户重定向到html文件

Javascript 如何从express\nodeJS中的post请求中获取数据,并在提交后将用户重定向到html文件,javascript,html,node.js,Javascript,Html,Node.js,我只是红了这个 我的快递代码是 var express= require('express'); var app= express(); var path= require('path'); app.use(express.static(path.join(__dirname,'public'))) //one way to server file //app.use(express.static(__dirname,'/public')); app.get('/',function(requ

我只是红了这个

我的快递代码是

var express= require('express');
var app= express();
var path= require('path');
app.use(express.static(path.join(__dirname,'public'))) //one way to server file
//app.use(express.static(__dirname,'/public'));

app.get('/',function(request, response){
    response.send("responde send");



})
app.get('/userName',function(request, response){
    response.send(request.query["userName"]);

})
app.post('/userName1',function(request,response){
    response.send(request.body.userName);
    console.log(request.body['userName']);

})



var server= app.listen(3001,function(){
    var host = server.address().address
    var port = server.address().port
console.log("listening "+port+ "port while the host"+host);
})
现在使用以下代码处理get请求 html是

但我在这里学习时不工作。
当用户提交表单时,请告知我如何将用户重定向到任何html文件?

该链接已过时。ExpressJS现在使用主体解析器作为语法分析器,这是正确解析表单数据所需要的

至于重定向,您可以使用javascript:

window.location = "https://www.google.com"
或者,在服务器上设置重定向标头:

res.setHeader("Location", "http://somewhere.com")
这与:

res.location("http://somewhere.com")

我使用的是Express4的体解析器。因此,您需要将主体解析器分配给Express,然后,您可以执行以下操作:

app.post("userName1", function(req, res){

  var username = req.body.userName;

});
是以粗体的方式使用的,路径信息是发送的,输入的表格名称是分别发送的

至于重定向,您可以使用:res.redirect(“URL”),或:

res.send(“JAVASCRIPT重定向代码”)

希望它对你有用:)

你必须像下面这样使用

    res.send("<script> window.location = 'http://localhost:8080'; </script>");
res.send(“window.location=”http://localhost:8080'; ");

上面的代码对我有用。也希望您能这样。

对不起,我不明白:(您能重新表达您的句子吗?非常感谢您的回答。window.location可以用于html文件?它位于我的项目根目录中。您指定了url直接用于重定向,我希望在提交时提供html文件
res.location("http://somewhere.com")
app.post("userName1", function(req, res){

  var username = req.body.userName;

});
res.send("<script> JAVASCRIPT REDIRECT CODE </script>")
    res.send("<script> window.location = 'http://localhost:8080'; </script>");