Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to post a form in react js and node js?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 .

>Solution :

You are missing the /api prefix at the form tag

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading