Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

express req body undefined

const express = require("express");
const app = express();
const port = 3002;
app.use(express.json());  // you need the body parser middleware 
// which already comes in express as a method

app.get("/", (req, res) => res.send("Hello World!"));

app.post('/', function (req, res) {
  const { data } = req.body;

  // post:
  // {
  //   "data": {
  //     "name": "Jhon",
  //     "age": 25
  //   }
  // }

  res.send('POST request to the homepage')
})

app.listen(port, () => console.log(`Example app listening on port ${port}!`));
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #express #req #body #undefined
ADD COMMENT
Topic
Name
4+1 =