Answers for "req.body"

13

body parser express

//make sure it is in this order
npm i body-parser

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

const app = express()

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

app.use(function (req, res) {
  res.setHeader('Content-Type', 'text/plain')
  res.write('you posted:\n')
  res.end(JSON.stringify(req.body, null, 2))
})
Posted by: Guest on July-21-2020
1

parse json express

// Update for Express 4.16+
// Starting with release 4.16.0, a new express.json() middleware is available.
var express = require('express');
var app = express();

app.use(express.json());

app.post('/', function(request, response){
  console.log(request.body);      // your JSON
   response.send(request.body);    // echo the result back
});

app.listen(3000);
Posted by: Guest on May-09-2020
2

urlencoded json express

var express = require('express')

var app = express()

app.use(express.json()) // for parsing application/json
app.use(express.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded

app.post('/profile', function (req, res, next) {
  console.log(req.body)
  res.json(req.body)
})
Posted by: Guest on September-30-2020
0

how to access the req.body

const express = require('express')
const app = express()

app.use(
  express.urlencoded({
    extended: true
  })
)

app.use(express.json())
Posted by: Guest on May-02-2021
0

req.body

(req.body, ' ' , ' ') --> here req is the parameter of your function and using this parameter your can access the properties over then url.
so look this example
suppose this is form 
<form>
enter the name : <input type="text" name="name">
<button type ="submit"> submit </button> 
</form>

so if you want to access the name -- which is filled by the user end.
so for this you can 
do like this->   console.log(req.body.name);  -- this will print the name (property) in console.
Posted by: Guest on May-16-2021

Browse Popular Code Answers by Language