0

With any luck, all I need is assistance updating deprecated body-parser node module?

I have an AWS instance running the bitnami Parse-Server installed through the AWS marketplace as the backend for an iOS app. The app crashed the other day even though no code changes where made in the iOS code nor the backend javascript running on AWS. The parse server log had the error message below coinciding with when the app crashed, which suggests that the deprecation of body parser is the cause of the issue. I researched this and changed the code in my server.js to what I believed was the new syntax but that didn't fix the problem as I got the error again after updating my server.js and restarting the server.

error: Forever detected script was killed by signal: SIGKILL
Sat, 24 Aug 2019 03:17:04 GMT body-parser deprecated bodyParser: use individual json/urlencoded middlewares at server.js:117:11
Sat, 24 Aug 2019 03:17:04 GMT body-parser deprecated undefined extended: provide extended option at node_modules/body-parser/index.js:105:29
parse-server running on port 1337
parse-dashboard running on port 4040

WARNING, Unable to connect to 'https://bigdayof.kerrydan.com/parse'. Cloud code and push notifications may be unavailable!

error: Forever detected script was killed by signal: SIGKILL
parse-server running on port 1337
parse-dashboard running on port 4040

WARNING, Unable to connect to 'https://bigdayof.kerrydan.com/parse'. Cloud code and push notifications may be unavailable!

I can supply the entire server.js but the code is long since it serves a web version of the app as well as the backend for the iOS app. This is the current code at the server.js that the log calls out at 117 for having the wrong syntax.

  app.use(bodyParser.json());

  app.use(bodyParser.urlencoded({
    extended: true
  }));

The log also references node_modules/body-parse/index.js line 105 and the code at that location.

/**
 * Create a middleware to parse json and urlencoded bodies.
 *
 * @param {object} [options]
 * @return {function}
 * @deprecated
 * @public
 */

function bodyParser (options) {
  var opts = {}

  // exclude type option
  if (options) {
    for (var prop in options) {
      if (prop !== 'type') {
        opts[prop] = options[prop]
      }
    }
  }

  var _urlencoded = exports.urlencoded(opts)
  var _json = exports.json(opts)

  return function bodyParser (req, res, next) {
    _json(req, res, function (err) {
      if (err) return next(err)
      _urlencoded(req, res, next)
    })
  }
}

I suspect that I had to do more than update the server.js and restart but I am not very experienced at backend programming for my iOS apps. Perhaps I need to completely re-install the body-parse package and that will update the node_module? Or maybe there are additional things I needed to change in the server.js?

Any advice or help would be greatly appreciated. Feel free to ask for any additional detail and I will add to this post with any details that I may not have known to include here.

Thank You.

Daniel Patriarca
  • 361
  • 3
  • 20
  • Take a look in this question: https://stackoverflow.com/questions/24330014/bodyparser-is-deprecated-express-4 – Davi Macêdo Aug 24 '19 at 20:18
  • I read that post. It suggests doing what is already done in my code (breaking it out into 2 calls and ensuring the extended argument is explicit). From that I would conclude this is not the same as my issue. thanks for the post though, appreciate any help I can get – Daniel Patriarca Aug 25 '19 at 00:21
  • Can you check if you have a `bodyParser()` call in any other place of your code? – Davi Macêdo Aug 25 '19 at 16:37

3 Answers3

0

Try to use bodyParser.json unless you have some options to pass in the json() function.

Gwamaka Charles
  • 1,479
  • 9
  • 12
0

In a terminal, run npm install —save body-parser. Import it on your project:

const bodyparser = require(‘body-parser’)

And use it as a middleware

app.use(bodyparser.json())

The old bodyParser (with P) is deprecated. You can see it even with the intellisense in VSCode

0

Don't use body-parser anymore

Since Express 4.16+ the body parsing functionality has become built into express

You can do

app.use(express.urlencoded({extended: true}));
app.use(express.json()) // To parse the incoming requests with JSON payloads

from directly express, without having to install body-parser

Abraham
  • 12,140
  • 4
  • 56
  • 92