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

标签 javascript html node.js

我刚刚红色了这个post

我的 express 代码是

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

现在使用此代码处理获取请求 html 是

<form action="/userName" method="get">
    <input type="text" name="userName">
    <input type="submit">

</form>

对于发布请求,我的 html 是

<form action="/userName1" method="post">
    <input type="text" name="userName">
    <input type="submit">

</form>

但是问题是如何获取post请求数据? 我已经尝试过这两行

 response.send(request.body.userName);
 console.log(request.body['userName']);

但此时不工作 link code is same我在哪里学习。 还告诉我当用户提交表单时如何将用户重定向到任何 html 文件?

最佳答案

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

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

window.location = "https://www.google.com"

或者,在服务器上设置重定向 header :

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

这与:

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

关于javascript - 如何从express\nodeJS中的post请求获取数据并在提交后将用户重定向到html文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26614146/

相关文章:

javascript - 使用每个 es6 对象检查每个属性的条件

javascript - 如何在发生适当的 DOM url 更改时运行 content.js 脚本?

javascript - 类型错误 : "method" is not a function

javascript - 如何自动调用 RESTful Web 服务

javascript - 仅在初始渲染后才 react CSS 动画

javascript - 按钮循环生成多个 Jquery 日期选择器

javascript - 如何在 Chrome 应用程序中存储 map 对象?

javascript - 如何为一个元素禁用 JQuery 按键事件?

html - 如何卸载 iframe 直到显示

javascript - 为什么我无法连接到 node-xmpp-bosh 服务器?