Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
100 views
in Technique[技术] by (71.8m points)

javascript - Can't receive body data on node.js

i'm developing a node.js api for a simple app for the first time and i'm confused about receiving data on body.

The code:


router.route("/ReceiveJSON").get((request, response) => {

    console.log(request.body);
    response.send("ok");
});

I'm sending this on Postman to test:

{
    "name": "test"
}

So i'm supposed to get this text on console, but i only get "{}" on console.

What am i doing wrong?

I dont know if this is causing the problem but i algo got the message "(node:11396) [DEP0066] DeprecationWarning: OutgoingMessage.prototype._headers is deprecated (Use node --trace-deprecation ... to show where the warning was created)" on console (but im not even using headers)

question from:https://stackoverflow.com/questions/65920180/cant-receive-body-data-on-node-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Hi you can try to use express like in this example:

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

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

app.use(express.json())

app.post('/todos', (req, res) => {
  console.log(req.body.todo)
})

I don't test it actually.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...