Filip
Filip

Reputation: 945

Body parser deprecated undefined extended: provide extended option

I am getting this error when trying to run my code and don't really know how to solve it. I don't really know the codebase because I'm new to it, so I'm completely lost and have no idea what to do.

body-parser deprecated undefined extended: provide extended option index.js:20:20
COMMON_CONFIG.mode === "development" false
Listening on port undefined

edit: package.json:

**{
  "name": "audio-guide-backend",
  "version": "0.0.0",
  "main": "index.js",
  "repository": "https://gitlab.com/islandica/audio-guide-backend.git",
  "license": "MIT",
  "private": true,
  "scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js"
  },
  "dependencies": {
    "crypto-random-string": "^3.0.1",
    "dotenv": "^8.0.0",
    "ejs": "^2.6.2",
    "express": "^4.17.1",
    "express-session": "^1.16.2",
    "pg": "^7.11.0"
  },
  "devDependencies": {
    "nodemon": "^1.19.1"
  }
}
**

Edit 2: This is how my index.js looks like:

require("dotenv").config();
const express = require("express");
const http = require("http");
const session = require("express-session");
const bodyParser = require("body-parser");
const { SERVER_CONFIG } = require("./config");

const app = express();

const server = http.createServer(app);

app.set("view engine", "ejs");

app.use(express.static("public"));
app.use((req, res, next) => {
  // if (req.url === "/api/login") console.log(req);
  next();
});
app.use(express.json());
app.use(bodyParser.urlencoded());
app.use(
  session({
    secret: "keyboard cat",
    resave: false,
    saveUninitialized: true
  })
);

// ROUTES
require("./routes")(app);

// NOT FOUND
app.use(function(req, res, next) {
  res.status(404);

  res.format({
    html: function() {
      res.render("404");
      // res.render("404", { url: req.url });
    },
    json: function() {
      res.json({ message: "Not found" });
    },
    default: function() {
      res.type("txt").send("Not found");
    }
  });
});

// INTERNAL ERROR
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.format({
    html: function() {
      res.render("500");
    },
    json: function() {
      res.json({ message: "server error: " + err.message });
    },
    default: function() {
      res.type("txt").send("server error: " + err.message);
    }
  });
  res.send("server error: " + err.message);
  /*res.render("500", {
    message:
      COMMON_CONFIG.mode === "development"
        ? err.message
        : "Internal server error"
  });*/
});

server.listen(SERVER_CONFIG.port, err => {
  if (err) {
    console.log("Error occured");
  } else {
    console.log(`Listening on port ${SERVER_CONFIG.port}`);
  }
});

It looks like my post is mostly code, so I'm adding more details.

Upvotes: 1

Views: 2244

Answers (2)

Krishna Vedula
Krishna Vedula

Reputation: 1791

Since the bodyParser is deprecated, I had resolved this issue by setting the extended:true option on the app.

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

instead of

app.use(bodyParser.urlencoded());

Upvotes: 0

David Kamer
David Kamer

Reputation: 2885

Listening on port undefined

This means that you don't have the port set. I see you're using dotenv, so the first place to check is your .env file in the root directory. If one wasn't created, make it and add whatever the name is for the environmental variable referenced in index.js. It's likely PORT.

Next you'll want to review the options you're passing into express.json() or whatever body-parser function you're calling. It's passing an undefined into that also.

Check your .env file or your system environmental variables and add the ones needed.

EDIT:

Check SERVER_CONFIG in the config file. That's where you should have you environmental variables that would usually be in .env. It's possible and likely that SERVER_CONFIG just consumes the dotenv package.

Also, you do not need const server = http.createServer(app); Just replace the call to server.listen(...) with app.listen(...)

Remove bodyParser, and use express.urlencoded({extended: true})

Upvotes: 2

Related Questions