DEV Community

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

Collapse
 
erknuepp profile image
erknuepp

I cannot get the test for the signup step to work in postman. I get errors for username, email and password. I have copied exactly what is in the github repo. It is also not adding an entry to the DB.

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

Collapse
 
dipakkr profile image
Deepak Kumar

Did you add/replace your username password in MongoURI ?

Collapse
 
erknuepp profile image
erknuepp

Yes, it is not a DB connection issue. No errors on the console.

Thread Thread
 
dipakkr profile image
Deepak Kumar

Try running the GitHub clone with your Db username password.

Thread Thread
 
yugyndprodigy10 profile image
Eugene Taabazuing

How do you now connect it to a frontend login/signup page?

Thread Thread
 
dipakkr profile image
Deepak Kumar

Hi Eugene,

You can simply call the API using axios or fetch to get data.

Here is a sample code snippet on how you can do it.

const config = {
            headers: {
                'Content-Type': "application/json"
            }
 };

const formData = {
input_username,
input_passwrd
};

const response = await axios.post('/user/login', formData, config);
Enter fullscreen mode Exit fullscreen mode

I hope this answers your question.

Thread Thread
 
yugyndprodigy10 profile image
Eugene Taabazuing

It does, thanks!

Thread Thread
 
grsarvesh profile image
GRSARVESH

Eugene, could you paste the full code oh how to link front end and back end and specify where it should be added

Collapse
 
lesupernom profile image
Alex

Old comment, but...

In Postman, go to the 'Headers' tab and set the 'Content-Type' value to 'application/json', by default it sends 'x-www-form-urlencoded'