Home > Blockchain >  Why is my post route not running properly in Express app?
Why is my post route not running properly in Express app?

Time:12-10

I am trying to add a route to '/signup' in my express application. But, every time I am sending a post request to the server it is resolving in "No response". Whereas the '/' route is working. Where have I gone wrong with the code?

index.js

import dotenv from "dotenv";
import cors from "cors";
import morgan from "morgan";
import dbConnect from "./config/dbConnect.js";
import { authRoute } from "./routes/auth.js";

dotenv.config();
const port = process.env.PORT;
const DATABASE_URI = process.env.DATABASE_URI;
const app = express();

dbConnect();

app.get("/", (req, res) => {
    res.sendStatus(200);
});

app.use(express.json());
app.use(cors());
app.use(morgan("combined"));

app.use("/api/v1", authRoute);

app.listen(port, () => {
    console.log(`Server running at ${port}...`);
});

auth.js

import { Router } from "express";

const router = Router();

router.post("signup", (req, res) => {
    const password = req.body.password;
    console.log(password);
});

export { router as authRoute };

dbConnect.js

import mongoose from "mongoose";
import dotenv from "dotenv";

dotenv.config();

const DATABASE_URI = process.env.DATABASE_URI;

const dbConnect = () => {
    mongoose.set("strictQuery", false);
    mongoose
        .connect(DATABASE_URI)
        .then(() => {
            console.log("connected");
        })
        .catch((error) => {
            console.error(error);
        });
};

export default dbConnect;

I am using Postman to send URL encoded form data to test. But the server is returning "no response"

CodePudding user response:

router.post("signup", (req, res) => {
    const password = req.body.password;
    console.log(password);
});

The client doesn't get a response because you haven't written any code to send a response.

You completely ignore the object passed to res.

You don't call res.json or res.render or res.send or any of the other methods that would send a response.

CodePudding user response:

Seems like the problem was with my VSCode extension RapidApi client, tried using insomnia and it worked out fine. Sorry for the trouble!

  • Related