简体   繁体   中英

Node.js: Request.body undefined (with body-parser)

I am new to Node.js and I have been trying to post (GET method) a simple form, but it still shows that "request.body" is undefined, despite using body-parser, and having moved it above the "route declarations" as suggested by other answers in other questions.

This is the HTML: (y.html)

<body>
<form action="/boats" method="get">
    <input type="text" name="textField" id="text">
    <input type="submit" name="submit" value="submit" id="submit">
</form>

srv.js:

 var http = require("http");
var express = require("express");
var bodyp = require("body-parser");
var app = express();


app.listen(3000);
app.use(bodyp());
app.use(express.static('index'));

app.get('/boats', function(request, response) {

    response.send("You sent: " + request.body.textField);

});

I just don't know where the mistake could be.

Thank you in advance

GET request in HTTP doesn`t have body.

If you want get params from GET request, you can write

request.param.textField

instead

request.body.textField 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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