Home > front end >  How to post a form in react js and node js?
How to post a form in react js and node js?

Time:03-11

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 .

CodePudding user response:

You are missing the /api prefix at the form tag

  • Related