Basic Node and Express - Use body-parser to Parse POST Requests - Help!

I’ve been struggling with this challenge for awhile and every time I run the tests it says “The ‘body-parser’ middleware should be mounted” and “Timeout has occurred.” Here’s the link to my Glitch page: https://level-syrup.glitch.me. My code is:
var bodyParser = require(‘body-parser’) app.use(bodyParser.urlencoded({ extended: false}))
I’m confused too about how to install the body-parser in package.json. How do I do that or is it already done? I’ve read everything I can find on Node.js, Express, and body-parsers but still can’t figure out what I’m doing wrong. What am I missing?

2 Likes

Sorry! Here’s the correct link: https://glitch.com/edit/#!/level-syrup?path=myApp.js:10:0.

1 Like

Hmm, that is puzzling. Mine works with essentially the same syntax. Your Package JSON is fine as is.

I did note a few differences between your app.js and mine.

  1. You declare variables with “var”, whereas I used ES6 “const”

  2. You do not have semicolons at the end of your variable declarations.

Maybe make those changes and see if it corrects the error.

Thanks! I tried what you suggested and added “const” instead of “var” when I declared my variables and I added semicolons after them but now it’s saying “Timeout has occurred.” Any idea why?
Here’s the link: https://glitch.com/edit/#!/level-syrup?path=myApp.js:10:51

1 Like

your package.json has a … in it. see mine below.

{
“name”: “fcc-learn-node-with-express”,
“version”: “0.1.0”,
“dependencies”: {
“express”: “^4.14.0”,
“body-parser”: “^1.15.2”,
“cookie-parser”: “^1.4.3”,
“fcc-express-bground”: “https://github.com/Em-Ant/fcc-express-bground-pkg.git
},
“main”: “server.js”,
“scripts”: {
“start”: “node server.js”
},
“engines”: {
“node”: “4.4.5”
}
}

@donnacamos88

The instruction is misleading in a way that you don’t need to

Install the body-parser module in your package.json.

It has already been done for you in the package.json, you can look under your dependencies key.

You can simply require it at the top of your myApp.js

1 Like

I too had the same problem. I removed cookie-parser from the dependencies in package.json and everything worked fine!

4 Likes

You just need to declare the bodyParser at the top of your file :

var bodyParser = require(“body-parser”);

/** 11) Get ready for POST Requests - the body-parser */
// place it before all the routes !

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

It works for me, I hope it help :smiley:

Read this hint helped me so much: freeCodeCamp Challenge Guide: Use body-parser to Parse POST Requests

1 Like

i think that’s the fix. thank you