简体   繁体   中英

Why Post Request Body is Empty? Node

I'm creating a adduser and delete user functionality using express in nodejs. I have users.json file for storing all the users. 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 localhost:8081/adduser --data '{"user":{ "name": "mohit", "password": "password4", "profession": "teacher", "id": 4 }'

Which says curl: (52) Empty reply from server.

Also on the other side req.body is always empty. 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.

thanks. ✌️

You need to parse the body. Try adding 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

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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