-1
I am learning how to create a simple API with Node. The get
works perfectly, but the post
ends up going like null
.
The code goes like this
const express = require('express');
const server = express();
server.use(express.json()); // faz com que o express entenda JSON
const geeks = [
{
'nome': 'ricardo'
}
]; // As informações ficaram armazenadas dentro deste array []
server.get('/geeks', (req, res) => {
return res.json(geeks);
}) // rota para listar todos os geeks
server.get('/geeks/:index', (req, res) => {
return res.json(req.user);
})
server.post('/geeks', (req, res) => {
// assim esperamos buscar o name informado dentro do body da requisição
const { name } = req.body;
geeks.push(name);
return res.json(geeks); // retorna a informação da variável geeks
})
server.listen(8080)
how you are sending the request?
– novic
I’m tested by Postman
– Ricardo Baltazar
spend the way you’re doing!?
– novic
I run the POST for http://localhost:8080/geeks. the header has selected the content-type application/x-www-form-urlencoded , and the body esta x-wwww-form-urlencoded.
– Ricardo Baltazar
has to be json!
– novic
got it. I changed the body to apliccation/json and managed to send the post. thanks!!
– Ricardo Baltazar