Pass data from reactjs to koajs server(405 method not allowed)

Viewed 15

I'm a beginner at both ReactJS and KoaJS.

I'm trying to send data from my React js form inputs to the server-side which was written with Koa.

Here is what I wrote to post data to the backend.

  • app.js

    const FormContainer = () => {
        const url = "http://localhost:3100/" 
        const [data,setData] = useState({
            email:"",
            firstname:"",
            lastname:""
        })
        
        function submit(e){
            e.preventDefault();
            Axios.post(url, {
                email: data.email,
                firstname: data.firstname,
                lastname: data.lastname
            })
            .then(res => {
                console.log(res.data)
            })
        }
        
        function handle(e){
            const newData = {...data}
            newData[e.target.id] = e.target.value
            setData(newData)
            console.log(newData);
        }

Here is the server-side code

  • server.js

 const Koa = require('koa');
    const json = require('koa-json');
    const KoaRouter = require('koa-router');
    const bodyParser = require('koa-bodyparser');
    const { default: App } = require('next/app');
    const cors = require('@koa/cors');
    
    const port = 3100
    const server = new Koa();
    const router = new KoaRouter();
    
    server.use(json());
    server.use(cors());
    
    router.get('/', ctx => (ctx.body = {
    
    }));
    
    server.use(router.routes());
    server.use(router.allowedMethods());
    
    server.listen(port, () => console.log('Server Running'));

When I run the server, I keep getting this console error but I'm not sure what's wrong. Can someone help me? Thank you. Error Image

0 Answers
Related