DEV Community

Discussion on: Authentication in NodeJS With Express and Mongo - CodeLab #1

Collapse
 
ts22082 profile image
Thomas W. Smith • Edited

I was getting the same error. i fixed it by adding

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

under

app.use(bodyParser.json());

index.js ends up looking like this:

const express = require("express");
const bodyParser = require("body-parser");
const user = require("./routes/user");
const InitiateMongoServer = require("./config/db");

// Initiate Mongo Server
InitiateMongoServer();

const app = express();

// PORT
const PORT = process.env.PORT || 4000;

// Middleware
app.use(bodyParser.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());

app.get("/", (req, res) => {
  res.json({ message: "API Working" });
});

/**
 * Router Middleware
 * Router - /user/*
 * Method - *
 */
app.use("/user", user);

app.listen(PORT, (req, res) => {
  console.log(`Server Started at PORT ${PORT}`);
});

Collapse
 
megumievans profile image
megumiEvans • Edited

I have the same issue "cannot get" I think that everything is well configured in postman and I add this lines suggested but still it didn't work... and I'm testing with the original repo I can connect to my db but I can't get anything with submit