0

I am trying to set up a very simple javascript server however I cant even properly get the data from a post request!

Here is what I am doing. I have annotated what works and what doesn't. Essentially everything except for the post request works perfectly. Unfortunately the body of the request is always empty resulting in garbage information.

const MongoClient = require('mongodb').MongoClient;
const express = require('express');
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.urlencoded({ extended: false }))


const mongoUrl = '<DBAddress Goes Here>';
MongoClient.connect(mongoUrl, (err, mongoDb) => {
  if(!err) {
    db = mongoDb;
    console.log("Connected correctly to server");//This always happen successfully
  }
});

app.listen(80);


app.get('/test', function(req, res) {
  res.json({ data1: 11, data2: 4, data3: 9 }); //This always works!
});


app.post('/update', function(req, res) {
  const params = req.body;
  console.log(req.body);//Empty
  console.log("Parameters");
  const newReport = {
    id: params.id,
    data: params.data
  };
  console.log(newReport);//Nothing is put in here
});

I am testing this post request in Postman with website.com/update as the address and the proper fields in the body part of the post.

J.Doe
  • 1,502
  • 13
  • 47

1 Answers1

1

You need to parse request body in order to get the body in req.body.

As you are already using body-parser package just add the following line after your urlEncoded middleware. and remember the order of middleware matters in the express.

app.use(bodyParser.json());

add above line right after this

app.use(bodyParser.urlencoded({ extended: false }))

And make sure that you are sending data in the JSON format as by default postman send data in plain format

enter image description here

Ridham Tarpara
  • 5,970
  • 4
  • 19
  • 39
  • One would expect that to work however it does not. I put it right after the listen line and it makes no change (with or without a semicolon after the first line) – J.Doe Feb 12 '18 at 08:52
  • you just have to add `app.use(bodyParser.json());` after your urlencoded middleware line. I updated answer for better clarity. – Ridham Tarpara Feb 12 '18 at 08:56
  • Top of my code now looks like this: `const MongoClient = require('mongodb').MongoClient; const express = require('express'); const bodyParser = require('body-parser'); const app = express(); app.use(bodyParser.urlencoded({ extended: false })) app.use(bodyParser.json());` and it is still not working – J.Doe Feb 12 '18 at 09:11
  • 1
    are you sure that you are sending request body data from postman in json format? can you show how you are sending req form postman. – Ridham Tarpara Feb 12 '18 at 09:48
  • 1
    Ah! That was it! I thought postman would be sending the data in JSON format by default since its interface was setup to look just like it however it was not. Things started working once I went to raw and selected JSON as the format. Thankyou. Could you please update your answer to reflect both the note about the lines of code and about setting postman up right so I can accept it? Thanks! – J.Doe Feb 12 '18 at 09:54
  • Also, I was wondering why does the app.use line not need a semicolon? – J.Doe Feb 12 '18 at 18:50
  • In javascript semicolon is optional but it is very good practice to use it. you can read more at https://stackoverflow.com/questions/444080/do-you-recommend-using-semicolons-after-every-statement-in-javascript. few answers really make points – Ridham Tarpara Feb 13 '18 at 04:37