Home > Enterprise >  express post method and body.req does not work
express post method and body.req does not work

Time:02-24

I am trying to get "POST" method form data from HTML form using app.post method but I can catch anything to req.body. What am I doing wrong?

My html form -

<form method="POST" action="/success.html">
  <div id="input-area">
    <textarea id="title" name="title" rows="1" cols="75" placeholder="Title" onkeyup="instance.tweaker()"></textarea>
    <textarea rows="10" type="text" cols="75" placeholder="Description" name="description"></textarea>
    <div id="calender"><input id="dater" type="date" value=""></div>
    <button type="submit" value="Submit"  id="buton">Add Task</button>
  </div>
</form>
<script src="backend2.js" defer></script>

my js code

const express = require('express');
const bodyParser = require('body-parser');
const app = express();

app.listen(3308,() =>{
    console.log('My server is running');
})

const jsonParser = bodyParser.json();

//

const urlencodedParser = bodyParser.urlencoded({ extended: false })

app.post('/success.html', urlencodedParser , function (req, res) {
    console.log(req.body.title);
})

CodePudding user response:

I reproduced your code without any error. To test it quickly, i send the index.html as the default route.

const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');

const port = 3308;
const app = express();

const urlencodedParser = bodyParser.urlencoded({ extended: false });

app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, '/index.html'));
});

app.post('/success.html', urlencodedParser, (req, res) => {
    res.send(req.body.title);
});

app.listen(port, () => {
    console.info(`server running on port ${port}`);
});
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Upload Example</title>
  </head>
  <body>
    <form method="POST" action="/success.html">
      <div id="input-area">
        <textarea
          id="title"
          name="title"
          rows="1"
          cols="75"
          placeholder="Title"
          onkeyup="removedForExample"
        ></textarea>
        <textarea
          rows="10"
          type="text"
          cols="75"
          placeholder="Description"
          name="description"
        ></textarea>
        <div id="calender"><input id="dater" type="date" value="" /></div>
        <button type="submit" value="Submit" id="buton">Add Task</button>
      </div>
    </form>
  </body>
</html>

CodePudding user response:

First of all, I am not sure which version of Express you are using but if you are using version 4.0 or up, you don't really need to import body-parser separately.

Also, you may re-order the code. Why don't you try like this?


const jsonParser = bodyParser.json();
const urlencodedParser = bodyParser.urlencoded({ extended: false })

app.post('/success.html', urlencodedParser , function (req, res) {
    console.log(req.body.title);
})

app.listen(3308,() =>{
    console.log('My server is running');
})

  • Related