简体   繁体   English

为什么发布请求正文为空? 节点

[英]Why Post Request Body is Empty? Node

I'm creating a adduser and delete user functionality using express in nodejs.我正在使用 nodejs 中的 express 创建添加用户和删除用户功能。 I have users.json file for storing all the users.我有 users.json 文件用于存储所有用户。 consider the following code.考虑以下代码。

var express = require('express');
var app = express();
var fs = require("fs");

//list users 
app.get('/listUsers', function(req, res) {
    fs.readFile(__dirname + "/" + "users.json", 'utf8', function(err, data) {
        console.log('List users ');
        res.send(data);
    });
})

//add user
app.post('/addUser', function(req, res) {
    // First read existing users.
    console.log('adding a new user');

    console.log(req.body);
    // fs.readFile(__dirname + "/" + "users.json", 'utf8', function(err, data) {
    //     data = JSON.parse(data);
    //     new_ = req.body;
    //     console.log(new_);
    //     // data["user4"] = new_["user"];
    //     // console.log(data);
    //     console.log('adding a new user')
    //     res.send(JSON.stringify(data));
    // });
})

//show user
app.get('/:id', function(req, res) {
    // First read existing users.
    fs.readFile(__dirname + "/" + "users.json", 'utf8', function(err, data) {
        var users = JSON.parse(data);
        console.log('show user ')
        var user = users["user" + req.params.id]
            // console.log(user);

        res.send(JSON.stringify(user));
    });
})

//delete user
app.delete('/deleteUser', function(req, res) {
    // First read existing users.
    fs.readFile(__dirname + "/" + "users.json", 'utf8', function(err, data) {
        data = JSON.parse(data);
        delete data["user" + req.params.id];
        console.log('deleting a user ');
        // console.log(data);
        res.send('user deleted');
    });
})


var server = app.listen(8081, function() {
    var host = server.address().address
    var port = server.address().port
    console.log("Example app listening at http://%s:%s", host, port)
})

I'm using curl to test this.我正在使用 curl 进行测试。

curl localhost:8081/adduser --data '{"user":{ "name": "mohit", "password": "password4", "profession": "teacher", "id": 4 }' curl localhost:8081/adduser --data '{"user":{ "name": "mohit", "password": "password4", "professional": "teacher", "id": 4 }'

Which says curl: (52) Empty reply from server.其中说 curl: (52) 来自服务器的空回复。

Also on the other side req.body is always empty.同样在另一边 req.body 总是空的。 Please help me fix this.请帮我解决这个问题。

Also if there is any efficient way of this operations then please let me know.另外,如果有任何有效的操作方法,请告诉我。

for adduser I want to add data provided by the request to users.json file for deleteuser I want to delete the user in which the id matches from the request.对于 adduser 我想将请求提供的数据添加到 users.json 文件对于 deleteuser 我想从请求中删除 id 匹配的用户。

thanks.谢谢。 ✌️ ✌️

You need to parse the body.你需要解析身体。 Try adding app.use(express.json())尝试添加app.use(express.json())

var express = require('express');
var app = express();
var fs = require("fs");

app.use(express.json());

If you use an older express version below 4.16 then use body parser如果您使用低于 4.16 的旧 express 版本,则使用正文解析器

Install it:安装它:

npm i body-parser

Use it:用它:

var express = require('express');
var app = express();
var fs = require("fs");
var bodyParser = require("body-parser");

app.use(bodyParser.json());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM