1
When I try to return one req.body.qualquerVariavel
acquired through a POST method, the return is undefined
, and if I put the API to show on the console, it shows the value correctly, I wanted to know if it is possible to return such value, thanks in advance.
<script>
document.getElementById('b').onload = async e => {
const url = 'http://localhost:8080/teste'
const data = new FormData()
data.append('nome', 'Eric ')
const options = {
method: 'POST',
body: new URLSearchParams(data)
}
fetch(url, options)
.then(resposne => JSON.stringify(resposne))
.then((err, val) => {
if(val) {
console.log(val)
} else {
console.log(err)
}
})
.catch(e => console.log(e))
}
</script>
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const cors = require('cors')
const http = require('http')
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, POST')
app.use(cors)
next()
})
app.post('/teste', (req, res) => {
const nomeCompleto = req.body.nome + 'Carvalho'
console.log(nomeCompleto)
return res.status(200).send({
nome: nomeCompleto
})
})
const server = http.createServer(app)
server.listen(8080)
How are you sending? How’s the code in the Back End Express? It’s too vague
– novic

app.post('/teste', (req, res) => { 
 const nomeCompleto = req.body.nome + 'Carvalho'
 console.log(nomeCompleto)
 return res.status(200).send({
 nome: nomeCompleto
 })
})

– EricOak

 const options = {
 method: 'POST',
 body: new URLSearchParams(data)
 }

 fetch(url, options)
 .then(resposne => JSON.stringify(resposne))
 .then((err, val) => {
 if(val) {
 console.log(val)
 } else {
 console.log(err)
 }
 })
 .catch(e => console.log(e))
 }
– EricOak
this is a code summarized of what I was trying to use, sorry for anything, I’m new on the platform.
– EricOak
ask the question, here are comments it is difficult to interpret
– novic
Okay, I’ll try to rephrase the question
– EricOak
ready, edited question
– EricOak