All the other routes are working except the one route "contactus" is shows me the cannot POST /be/cotnactus while working with middlewares

Viewed 17

I'm trying to make a POST request route "/be/contactus" by using the middleware and express.Router(), but it is showing me the error 404 "cannot POST /be/contactus"

/** @format */

import bodyParser from 'body-parser';
import cors from 'cors';
import express from 'express';
import { connectToMongoose } from './functions/connectToMongodb';
import routes from './routes/routes';

const BACKEND_PORT = process.env.BACKEND_PORT;
const app = express();
app.use(cors());
app.use(bodyParser.urlencoded({
  extended: true
}));

/**
 * Connect to mongodb database
 */

connectToMongoose();

app.use('/be/', routes());

app.listen(BACKEND_PORT, () => {
  console.log('App is listening on ', BACKEND_PORT);
});

route file is

  /** @format */

import { Request, Response, Router } from 'express';
import { authRouter } from './login/index';
import { surveysRouter } from './surveyList/index';
import { mediaRouter } from './media/index';
import { questionsRouter } from './question';
import { appearanceRouter } from './appearancesurvey';
import { surveyListRouter } from './surveylistsetting/surveylistsetting';
import { videoRouter } from './video';
import { imageRouter } from './image';
import { screenRouter } from './Screen';
import { imagequeRouter } from './imagequestions';
import { accountRouter } from './account';
import { surveysRoutertemplate } from './surveytemplate/index'
//import login from "../routes/login";
import { contactusRouter } from './contactus';

export default function () {
  let router = Router();
  router.get('/', (req: Request, res: Response) => {
    res.send('Basic setup for backend is complete');
  });


  /**
   * Login routes
   */

  router.use('/', authRouter);
  router.use('/', surveysRouter);
  router.use('/', accountRouter);
  router.use('/', mediaRouter);
  router.use('/', questionsRouter);
  router.use('/', appearanceRouter);
  router.use('/', surveyListRouter);
  router.use('/', appearanceRouter);
  router.use('/', videoRouter);
  router.use('/', imageRouter);
  router.use('/', screenRouter);
  router.use('/', imagequeRouter);
  router.use('/', surveysRoutertemplate);
  router.use('/', contactusRouter);
  return router;
}

the main issue is the last middleware which is "router.use('/', contactusRouter); The contactusRouter file:

import { Router } from "express";
import contactusController from "../../controller/contactus";

const contactusRouter: Router = Router();

contactusRouter.post('/contactus', contactusController.contactus);
  
export { contactusRouter };

In the contactusRouter file I'm trying to make the POST request to the route "/contactus" which is further passing the control to the contactusController file the contactusController file.

import { Request, Response, NextFunction } from 'express';
import contactusModel from '../models/contactus';
class contactusController {
  static contactus = async (req: Request, res: Response, next: NextFunction) => {
    try {
        const { category, name, email, subject, message, surveyId } = req.body;
        const contactNew = new contactusModel({
            Category: category,
            Name: name,
            Email: email,
            Subject: subject,
            Message: message,
            SurveyId: surveyId
        })
        const saveContact = await contactNew.save();
        
        res.status(200).json({ contact: saveContact });
    } catch (error) {
        console.log(error);
        res.status(400).json({ error: error });
    }
  };
}

export default contactusController;
0 Answers
Related