Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

124
Views
express post method and body.req does not work

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);
})
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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>
about 4 years ago · Juan Pablo Isaza Report

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!