When utilizing Axios to communicate with a backend server, an error message of "The requested resource could not be found (404)" may be displayed

Currently, I am developing a Web Application using React. The application features a login/register function for user authentication. To achieve this, I have set up an express server to handle HTTP requests to interact with a MYSQL database. Two specific endpoints, '/register' and '/login', have been created to manage the registration and login processes respectively. In order to connect the frontend to these endpoints, Axios is employed to post user data to the database efficiently.

In its current state, the application requires both the server and client files to be running on the same port for seamless operation. Unfortunately, if the page is refreshed, this setup can be disrupted. Therefore, I am seeking solutions to enable the posting of HTTP requests into the database without the dependency on having both frontend and backend components operating on the same port consecutively. Thank you!

Frontend:

import React, { useState } from "react";
import axios from 'axios';

// Code snippet...

Backend:

// Relevant backend code snippet...

Answer №1

You have encountered an issue with your post request due to using the wrong port. Your server is set up to run on port 4000, but you are making axios post requests on port 3000. To resolve this, make the following adjustments in your React code:

axios.post('http://localhost:4000/login', form)

and

axios.post('http://localhost:4000/register', form)

Sending requests from a different port than what the server is running on (in this case, 4000) can result in a CORS error. To fix this, you will need to install the cors package and configure it accordingly based on your project requirements. I hope this guidance helps you address the issue.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

React-Redux - sending an action to update multiple components

I am facing a dilemma where I'm not sure if my issue stems from missing the bigger picture or if it's a valid concern in redux... There is an SVG chart on my page with a fixed size. Sometimes, when the user opens a panel, the width of the chart ...

What could be causing the styled-component's flex value to not update?

I have a sidebar and main content on my website layout. The main content occupies most of the screen space, while the sidebar should only take up a small portion. Both elements are within a flexbox container, with the sidebar and main content as child divs ...

Click on the button to be directed to a different page using React

<div class="clickable-icon"> <button component={Link} to={"/create"}> <EditIcon></EditIcon> </button> </div> I have written this code snippet to turn an icon into a clickable button that directs ...

Error in creating the database: Outcome is not defined

I am currently working on developing a basic nodejs program using express and mysql. const express = require('express'); const mysql = require('mysql'); const db = mysql.createConnection({ host : 'localhost', user : ...

I am interested in learning more about the process of hosting and uploading a complete website that includes server configuration, React development, and MySQL integration

After completing my first full project using server, client, and data, I am eager to learn more about hosting and uploading a complete website that includes server-side code (Node.js + Express), MySql database, and a React app. I understand the process of ...

Struggling with the migration of routes from react-router v3 to v4

My route configuration using react-router v3 is as follows: <Route component={App}> <Route path="login" component={Login} /> <Route path="logout" component={Logout} /> <Route path="/" component={Admin}> <IndexRoute com ...

How can I use UNION in MySQL or MySQLi to select data without any NULL values and order the results in a specific

I am faced with a challenge in my coding project... $query = "SELECT {$selections} FROM {$table_1} UNION ALL SELECT {$selections} FROM {$table_2} UNION SELECT {$selections} FROM {$table_3} UNION SELECT {$selections} FROM {$table_ ...

What is a more efficient way to differentiate a group of interfaces using an object map instead of using a switch statement?

As someone still getting the hang of advanced typescript concepts, I appreciate your patience. In my various projects, I utilize objects to organize react components based on a shared prop (e.g _type). My goal is to automatically determine the correct com ...

The Body-Parser function returns an undefined value

In my server.js file, I have implemented user authentication using the following code: //API ROUTES var apiRoutes = express.Router(); //route to auth user apiRoutes.post('/authenticate', function(req,res){ //find the user User.findOne({ ...

Encountering difficulties while attempting to modify information in mongodb

Need help with updating specific fields in a Mongoose driver and Express JS setup. Here is the schema: var mongoose = require('mongoose'), Schema = mongoose.Schema; var ProfilesSchema = new Schema({ presentRound: { type: Numb ...

Struggling with Running the Webpack serve Command

Struggling to set up a React app using Webpack and Babel. After following various tutorials on YouTube and other websites, I keep encountering the same error whenever I run the webpack serve command, even if my index.js file is empty. [ERROR]: ERROR in .. ...

Is StyledEngineProvider necessary for utilizing the sx prop with React and Mui v5?

In my current project, I am utilizing react along with Material-UI version 5 (MUI) and exclusively utilizing the sx prop for styling purposes. My question is, is it necessary to utilize the StyledEngineProvider as a wrapper for my ThemeProvider? Thank yo ...

Exploring the intricacies of React's useEffect: Solving the challenge of updating data when two separate dependency arrays are

I am facing an issue with two different useEffect hooks where the dependency arrays are different. const [dateFilterSort, setDateFilterSort] = useState({ queryText: initialQueryText(params.sortName), cardText: initialCardText(params.sortName), ...

Using DraftJS to swap text while preserving formatting

Currently, I am implementing Draftjs with draft-js-plugins-editor and utilizing two plugins: draft-js-mathjax-plugin and draft-js-mention-plugin My goal is to replace all mentions with their corresponding values when the user uses '@' to mention ...

Issue with Mui custom step not displaying on the screen

I'm having trouble with the MUI stepper component in my project. The steps are not rendering properly. The stepper is located within a dialog box: const DialogForm = ({ open, activeStep, children }: { open: boolean; activeStep: number; children ...

When utilizing styled-jsx alongside postcss, experiencing issues with styles failing to reload or rebuild

I'm currently using postcss in conjunction with styled-jsx. In my setup, I have multiple CSS files that I'm importing using the @import directive within the _app.js file. Everything seems to work smoothly, except when I modify any of the CSS file ...

What is the procedure for initiating a POST request when the payload is ready for submission?

After a successful payment, my success page is displayed with a URL that includes a query parameter. I want to make a POST request to my API endpoint when the page loads, but I'm encountering an issue where the first POST request is empty (router.quer ...

Error in Next JS: Invalid element type provided. Expected a string for built-in components or a class/function for composite components

Issue encountered while integrating Leaflet JS with Next JS, facing an error indicating that the element type is invalid. Here's the dynamic import of my Map component. /components/MapDir/index.js import dynamic from 'next/dynamic'; const ...

What causes the error when I use "use client" at the top of a component in Next.js?

Whenever I include "use client" at the top of my component, I encounter the following error: event - compiled client and server successfully in 2.5s (265 modules) wait - compiling... event - compiled client and server successfully in 932 ms (265 modules) ...

Node.js Express not inserting data with Mongoose when using form data

For the past two weeks, I have been struggling to post data to insert into a database using form-data. It consistently shows a 400 bad request error. Below is my code for server.js: require('./db.js') let express = require('express') ...