0

I am trying to submit a form by method post but when I try to submit it, node gives me the error cannot POST .

Here is my code for form :

import React from 'react'
import styles from './SubmitForm.module.scss';
export const SubmitForm = () => {
  const navigate = useNavigate();
  
  return (
    <div className= {styles.container}>       
     <h2 className= {styles.heading}>Choose</h2>
        <form action = '/sub' method='post'>
            <button type='submit'></button>
        </form>
        </div>
  )
}

In my submit.controller.js:

const submission = () => 
{
 console.log("submitted")
}

In my submit.route.js :

const express = require('express')
const router = express.Router()

const {
submission} = require('../../controllers/submit.controller.js') ;

router.post('/sub' , submission)
module.exports = router;

And in my server.js :

const subRouter = require('./routes/submit.route)
app.use('/api' ,  subRouter)

Note : I am running my backend server at localhost: 5000 and front end at 3000 .

1 Answer 1

1

You are missing the /api prefix at the form tag

Sign up to request clarification or add additional context in comments.

3 Comments

Should I write 'api/sub' ?? And BTW my backend sserver is running at localhost 5000 and front end at 3000
Yes. You should
If you different host, you need to add full url at action attribute. So the url should localhost:5000/api/sub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.