Getting 404 (Not Found) on axios.delete()How to enable DELETE request?How do I get started with Node.jsHow do I get the path to the current script with Node.js?How to redirect 404 errors to a page in ExpressJS?How to get GET (query string) variables in Express.js on Node.js?How to get the full url in Express?How to access the GET parameters after “?” in Express?How to resolve Mongoose Connect UNKNOWN?PUT/ update operation fails in $resource AngularJS client in rest based app (mongoose insert / update issue).How nginx proxy to express(node.js)?Why res.data is index.html?Why express.Router() while separating routes

Am I storing my camera the wrong way, in a dry box with humidity between 37-48?

I reverse the source code, you reverse the input!

Why was it decided in 1956 to abolish the spelling чорт (devil) in favor of чёрт?

What exactly did this mechanic sabotage on the American Airlines 737, and how dangerous was it?

Why is 6. Nge2 better, and 7. d5 a necessary push in this game?

Interchange `colon` and `:`

Is the order of words purely based on convention?

Does "as soon as" imply simultaneity?

How to justify getting additional team member when the current team is doing well?

Can my former employer sue me if I don't give them the photos I took (taking pictures was not part of my job description)?

Passiflora Snow Queen is drying out

May I know how to stop these death waves?

Does every piano need tuning every year?

Would you write key signatures for non-conventional scales?

Difference between types of yeast

There are 51 natural numbers between 1-100, proof that there are 2 numbers such that the difference between them equals to 5

Which lens has the same capability of lens mounted in Nikon P1000?

Can someone give the intuition behind Mean Absolute Error and the Median?

How 象【しょう】 ( ≈かたち、 すがた、ようす) and 象【ぞう】 (どうぶつ) got to be written with the same kanji?

A famous scholar sent me an unpublished draft of hers. Then she died. I think her work should be published. What should I do?

If a spaceship ran out of fuel somewhere in space between Earth and Mars, does it slowly drift off to Sun?

How can this Stack Exchange site have an animated favicon?

practicality of 30 year fix mortgage at 55 years of age

Does the Horizon Walker ranger's Planar Warrior feature bypass resistance to non-magical attacks?



Getting 404 (Not Found) on axios.delete()


How to enable DELETE request?How do I get started with Node.jsHow do I get the path to the current script with Node.js?How to redirect 404 errors to a page in ExpressJS?How to get GET (query string) variables in Express.js on Node.js?How to get the full url in Express?How to access the GET parameters after “?” in Express?How to resolve Mongoose Connect UNKNOWN?PUT/ update operation fails in $resource AngularJS client in rest based app (mongoose insert / update issue).How nginx proxy to express(node.js)?Why res.data is index.html?Why express.Router() while separating routes






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I'm trying to implement the delete functionality for my survey creation app.



I'm using MongoDB with mongoose for the database, node.js/Express for the backend server and React/Redux for the frontend side.



Although I think I set routing correctly, I get 404 (Not Found) on axios.delete().



The error says that http://localhost:3000/api/surveys/delete/the-survey-id-here is not found.



I have been reading the documentation of axios, Express, mongoose and other websites, however, nothing worked for me.



I tried the following things.



  • Use findByIdAndRemove() instead of deleteOne()

  • Pass surveyId in the action creator const response = await axios.delete("/api/surveys", data: surveyId );

  • Use <a></a> instead of <button></button>

Here are my codes.



SurveyList.js (react component which has the delete button)



import fetchSurveys, deleteSurvey from '../../actions'

...

<div className="card-action">
<a className="red-text text-accent-1">Yes: survey.yes</a>
<a className="red-text text-accent-1">No: survey.no</a>
<button
className="btn-floating btn-small waves-effect waves-light red right"
onClick=() => this.props.deleteSurvey(survey._id)
>
<i className="material-icons">delete_forever</i>
</button>
</div>

...

const mapStateToProps = ( surveys ) =>
return surveys


export default connect(
mapStateToProps,
fetchSurveys, deleteSurvey
)(SurveyList)


actions/index.js (action creator)



export const deleteSurvey = (surveyId) => async dispatch => 
const response = await axios.delete(`/api/surveys/delete/$surveyId`)

dispatch( type: FETCH_SURVEYS, payload: response.data )



surveyRoute.js (routing handler)



app.delete('/api/surveys/delete/:surveyId', async (req, res) => 
await Survey.deleteOne( _id: req.params.surveyId )

const surveys = await Survey.find( _user: req.user.id ).select(
recipients: false
)
res.send(surveys)
)


server/index.js



const express = require('express')
const mongoose = require('mongoose')
const cookieSession = require('cookie-session')
const passport = require('passport')
const bodyParser = require('body-parser')
const keys = require('./config/keys')
require('./models/User')
require('./models/Survey')
require('./services/passport')

mongoose.connect(keys.mongoURI)

const app = express()

app.use(bodyParser.json())
app.use(
cookieSession(
maxAge: 30 * 24 * 60 * 60 * 1000, // milliseconds
keys: [keys.cookieKey]
)
)
app.use(passport.initialize())
app.use(passport.session())

require('./routes/authRoutes')(app)
require('./routes/billingRoutes')(app)
require('./routes/surveyRoutes')(app)

if(process.env.NODE_ENV === 'production')
app.use(express.static('client/build'))

const path = require('path')
app.get('*',(req, res) =>
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
)


const PORT = process.env.PORT || 5000
app.listen(PORT)









share|improve this question


























  • show your main app.js, where you're using surveyRoute.js

    – Marcos Casagrande
    Mar 28 at 18:12











  • Thank you for your comment. I added the main routing file at the bottom.

    – Kenta. K
    Mar 28 at 18:26











  • serving on port 3000 or 5000? what is you env.PORT value?

    – 1565986223
    Mar 28 at 18:40











  • env.PORT is passed from Heroku. Otherwise, npm run server will use 5000 and npm run client will use 3000.

    – Kenta. K
    Mar 28 at 19:05











  • You say client runs on 3000 and server runs on 5000, but you're making request to port 3000, meaning the client?

    – 1565986223
    Mar 28 at 19:08

















1















I'm trying to implement the delete functionality for my survey creation app.



I'm using MongoDB with mongoose for the database, node.js/Express for the backend server and React/Redux for the frontend side.



Although I think I set routing correctly, I get 404 (Not Found) on axios.delete().



The error says that http://localhost:3000/api/surveys/delete/the-survey-id-here is not found.



I have been reading the documentation of axios, Express, mongoose and other websites, however, nothing worked for me.



I tried the following things.



  • Use findByIdAndRemove() instead of deleteOne()

  • Pass surveyId in the action creator const response = await axios.delete("/api/surveys", data: surveyId );

  • Use <a></a> instead of <button></button>

Here are my codes.



SurveyList.js (react component which has the delete button)



import fetchSurveys, deleteSurvey from '../../actions'

...

<div className="card-action">
<a className="red-text text-accent-1">Yes: survey.yes</a>
<a className="red-text text-accent-1">No: survey.no</a>
<button
className="btn-floating btn-small waves-effect waves-light red right"
onClick=() => this.props.deleteSurvey(survey._id)
>
<i className="material-icons">delete_forever</i>
</button>
</div>

...

const mapStateToProps = ( surveys ) =>
return surveys


export default connect(
mapStateToProps,
fetchSurveys, deleteSurvey
)(SurveyList)


actions/index.js (action creator)



export const deleteSurvey = (surveyId) => async dispatch => 
const response = await axios.delete(`/api/surveys/delete/$surveyId`)

dispatch( type: FETCH_SURVEYS, payload: response.data )



surveyRoute.js (routing handler)



app.delete('/api/surveys/delete/:surveyId', async (req, res) => 
await Survey.deleteOne( _id: req.params.surveyId )

const surveys = await Survey.find( _user: req.user.id ).select(
recipients: false
)
res.send(surveys)
)


server/index.js



const express = require('express')
const mongoose = require('mongoose')
const cookieSession = require('cookie-session')
const passport = require('passport')
const bodyParser = require('body-parser')
const keys = require('./config/keys')
require('./models/User')
require('./models/Survey')
require('./services/passport')

mongoose.connect(keys.mongoURI)

const app = express()

app.use(bodyParser.json())
app.use(
cookieSession(
maxAge: 30 * 24 * 60 * 60 * 1000, // milliseconds
keys: [keys.cookieKey]
)
)
app.use(passport.initialize())
app.use(passport.session())

require('./routes/authRoutes')(app)
require('./routes/billingRoutes')(app)
require('./routes/surveyRoutes')(app)

if(process.env.NODE_ENV === 'production')
app.use(express.static('client/build'))

const path = require('path')
app.get('*',(req, res) =>
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
)


const PORT = process.env.PORT || 5000
app.listen(PORT)









share|improve this question


























  • show your main app.js, where you're using surveyRoute.js

    – Marcos Casagrande
    Mar 28 at 18:12











  • Thank you for your comment. I added the main routing file at the bottom.

    – Kenta. K
    Mar 28 at 18:26











  • serving on port 3000 or 5000? what is you env.PORT value?

    – 1565986223
    Mar 28 at 18:40











  • env.PORT is passed from Heroku. Otherwise, npm run server will use 5000 and npm run client will use 3000.

    – Kenta. K
    Mar 28 at 19:05











  • You say client runs on 3000 and server runs on 5000, but you're making request to port 3000, meaning the client?

    – 1565986223
    Mar 28 at 19:08













1












1








1








I'm trying to implement the delete functionality for my survey creation app.



I'm using MongoDB with mongoose for the database, node.js/Express for the backend server and React/Redux for the frontend side.



Although I think I set routing correctly, I get 404 (Not Found) on axios.delete().



The error says that http://localhost:3000/api/surveys/delete/the-survey-id-here is not found.



I have been reading the documentation of axios, Express, mongoose and other websites, however, nothing worked for me.



I tried the following things.



  • Use findByIdAndRemove() instead of deleteOne()

  • Pass surveyId in the action creator const response = await axios.delete("/api/surveys", data: surveyId );

  • Use <a></a> instead of <button></button>

Here are my codes.



SurveyList.js (react component which has the delete button)



import fetchSurveys, deleteSurvey from '../../actions'

...

<div className="card-action">
<a className="red-text text-accent-1">Yes: survey.yes</a>
<a className="red-text text-accent-1">No: survey.no</a>
<button
className="btn-floating btn-small waves-effect waves-light red right"
onClick=() => this.props.deleteSurvey(survey._id)
>
<i className="material-icons">delete_forever</i>
</button>
</div>

...

const mapStateToProps = ( surveys ) =>
return surveys


export default connect(
mapStateToProps,
fetchSurveys, deleteSurvey
)(SurveyList)


actions/index.js (action creator)



export const deleteSurvey = (surveyId) => async dispatch => 
const response = await axios.delete(`/api/surveys/delete/$surveyId`)

dispatch( type: FETCH_SURVEYS, payload: response.data )



surveyRoute.js (routing handler)



app.delete('/api/surveys/delete/:surveyId', async (req, res) => 
await Survey.deleteOne( _id: req.params.surveyId )

const surveys = await Survey.find( _user: req.user.id ).select(
recipients: false
)
res.send(surveys)
)


server/index.js



const express = require('express')
const mongoose = require('mongoose')
const cookieSession = require('cookie-session')
const passport = require('passport')
const bodyParser = require('body-parser')
const keys = require('./config/keys')
require('./models/User')
require('./models/Survey')
require('./services/passport')

mongoose.connect(keys.mongoURI)

const app = express()

app.use(bodyParser.json())
app.use(
cookieSession(
maxAge: 30 * 24 * 60 * 60 * 1000, // milliseconds
keys: [keys.cookieKey]
)
)
app.use(passport.initialize())
app.use(passport.session())

require('./routes/authRoutes')(app)
require('./routes/billingRoutes')(app)
require('./routes/surveyRoutes')(app)

if(process.env.NODE_ENV === 'production')
app.use(express.static('client/build'))

const path = require('path')
app.get('*',(req, res) =>
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
)


const PORT = process.env.PORT || 5000
app.listen(PORT)









share|improve this question
















I'm trying to implement the delete functionality for my survey creation app.



I'm using MongoDB with mongoose for the database, node.js/Express for the backend server and React/Redux for the frontend side.



Although I think I set routing correctly, I get 404 (Not Found) on axios.delete().



The error says that http://localhost:3000/api/surveys/delete/the-survey-id-here is not found.



I have been reading the documentation of axios, Express, mongoose and other websites, however, nothing worked for me.



I tried the following things.



  • Use findByIdAndRemove() instead of deleteOne()

  • Pass surveyId in the action creator const response = await axios.delete("/api/surveys", data: surveyId );

  • Use <a></a> instead of <button></button>

Here are my codes.



SurveyList.js (react component which has the delete button)



import fetchSurveys, deleteSurvey from '../../actions'

...

<div className="card-action">
<a className="red-text text-accent-1">Yes: survey.yes</a>
<a className="red-text text-accent-1">No: survey.no</a>
<button
className="btn-floating btn-small waves-effect waves-light red right"
onClick=() => this.props.deleteSurvey(survey._id)
>
<i className="material-icons">delete_forever</i>
</button>
</div>

...

const mapStateToProps = ( surveys ) =>
return surveys


export default connect(
mapStateToProps,
fetchSurveys, deleteSurvey
)(SurveyList)


actions/index.js (action creator)



export const deleteSurvey = (surveyId) => async dispatch => 
const response = await axios.delete(`/api/surveys/delete/$surveyId`)

dispatch( type: FETCH_SURVEYS, payload: response.data )



surveyRoute.js (routing handler)



app.delete('/api/surveys/delete/:surveyId', async (req, res) => 
await Survey.deleteOne( _id: req.params.surveyId )

const surveys = await Survey.find( _user: req.user.id ).select(
recipients: false
)
res.send(surveys)
)


server/index.js



const express = require('express')
const mongoose = require('mongoose')
const cookieSession = require('cookie-session')
const passport = require('passport')
const bodyParser = require('body-parser')
const keys = require('./config/keys')
require('./models/User')
require('./models/Survey')
require('./services/passport')

mongoose.connect(keys.mongoURI)

const app = express()

app.use(bodyParser.json())
app.use(
cookieSession(
maxAge: 30 * 24 * 60 * 60 * 1000, // milliseconds
keys: [keys.cookieKey]
)
)
app.use(passport.initialize())
app.use(passport.session())

require('./routes/authRoutes')(app)
require('./routes/billingRoutes')(app)
require('./routes/surveyRoutes')(app)

if(process.env.NODE_ENV === 'production')
app.use(express.static('client/build'))

const path = require('path')
app.get('*',(req, res) =>
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
)


const PORT = process.env.PORT || 5000
app.listen(PORT)






node.js reactjs mongodb express axios






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 30 at 14:42









halfer

15.3k7 gold badges63 silver badges129 bronze badges




15.3k7 gold badges63 silver badges129 bronze badges










asked Mar 28 at 18:10









Kenta. KKenta. K

361 silver badge7 bronze badges




361 silver badge7 bronze badges















  • show your main app.js, where you're using surveyRoute.js

    – Marcos Casagrande
    Mar 28 at 18:12











  • Thank you for your comment. I added the main routing file at the bottom.

    – Kenta. K
    Mar 28 at 18:26











  • serving on port 3000 or 5000? what is you env.PORT value?

    – 1565986223
    Mar 28 at 18:40











  • env.PORT is passed from Heroku. Otherwise, npm run server will use 5000 and npm run client will use 3000.

    – Kenta. K
    Mar 28 at 19:05











  • You say client runs on 3000 and server runs on 5000, but you're making request to port 3000, meaning the client?

    – 1565986223
    Mar 28 at 19:08

















  • show your main app.js, where you're using surveyRoute.js

    – Marcos Casagrande
    Mar 28 at 18:12











  • Thank you for your comment. I added the main routing file at the bottom.

    – Kenta. K
    Mar 28 at 18:26











  • serving on port 3000 or 5000? what is you env.PORT value?

    – 1565986223
    Mar 28 at 18:40











  • env.PORT is passed from Heroku. Otherwise, npm run server will use 5000 and npm run client will use 3000.

    – Kenta. K
    Mar 28 at 19:05











  • You say client runs on 3000 and server runs on 5000, but you're making request to port 3000, meaning the client?

    – 1565986223
    Mar 28 at 19:08
















show your main app.js, where you're using surveyRoute.js

– Marcos Casagrande
Mar 28 at 18:12





show your main app.js, where you're using surveyRoute.js

– Marcos Casagrande
Mar 28 at 18:12













Thank you for your comment. I added the main routing file at the bottom.

– Kenta. K
Mar 28 at 18:26





Thank you for your comment. I added the main routing file at the bottom.

– Kenta. K
Mar 28 at 18:26













serving on port 3000 or 5000? what is you env.PORT value?

– 1565986223
Mar 28 at 18:40





serving on port 3000 or 5000? what is you env.PORT value?

– 1565986223
Mar 28 at 18:40













env.PORT is passed from Heroku. Otherwise, npm run server will use 5000 and npm run client will use 3000.

– Kenta. K
Mar 28 at 19:05





env.PORT is passed from Heroku. Otherwise, npm run server will use 5000 and npm run client will use 3000.

– Kenta. K
Mar 28 at 19:05













You say client runs on 3000 and server runs on 5000, but you're making request to port 3000, meaning the client?

– 1565986223
Mar 28 at 19:08





You say client runs on 3000 and server runs on 5000, but you're making request to port 3000, meaning the client?

– 1565986223
Mar 28 at 19:08












3 Answers
3






active

oldest

votes


















0
















So if I undestood correctly, you have a Expressjs App and a client app running on different ports, if so I think I found the issue



You are making ajax calls as if you had both frontend and backend running on the same server. Here are two possible solutions




  1. Change the address of the Axios delete action to point to the right server if you are planning to run the backend and frontend on different servers



    app.delete('http://localhost:5000/api/surveys/delete/:surveyId', async (req, res) => 
    await Survey.deleteOne( _id: req.params.surveyId )

    const surveys = await Survey.find( _user: req.user.id ).select(
    recipients: false
    )
    res.send(surveys)
    )


    Off course you have to change the address once you deploy it to production and enable CORS




  2. Build the client app for producion, this process will compile all css, js and html and save it to client/build folder as stated on line



    res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));


    This will cause the frontend and backend code to run on the same server







share|improve this answer



























  • Thank you very much for your answer. It made me realize that there might be a problem with the proxy.

    – Kenta. K
    Mar 29 at 4:12


















0
















Thank you very much for all answers and comments. The problem was the proxy. I am using http-proxy-middleware to handle requests between different ports in the dev environment. All comments about ports made me realize that I need to check the 'setupProxy.js' file. I modified the code inside the file as below and that fixed the problem.



setupProxy.js



const proxy = require('http-proxy-middleware')

module.exports = function(app)
app.use(proxy ('/auth/google', target: 'http://localhost:5000' ))
app.use(proxy ('/api/**', target: 'http://localhost:5000' ))



Thank you very much again for all the answers and comments.






share|improve this answer
































    -2
















    CORS (Delete verb) must be enabled in your route middleware.



    This thread may help you.






    share|improve this answer

























    • This could be the case but Axios is not calling the right address

      – Erick Castrillo
      Mar 28 at 19:44











    • Thank you for your answer. The problem was the proxy. It didn't handle the request properly.

      – Kenta. K
      Mar 29 at 4:14













    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );














    draft saved

    draft discarded
















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55404279%2fgetting-404-not-found-on-axios-delete%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0
















    So if I undestood correctly, you have a Expressjs App and a client app running on different ports, if so I think I found the issue



    You are making ajax calls as if you had both frontend and backend running on the same server. Here are two possible solutions




    1. Change the address of the Axios delete action to point to the right server if you are planning to run the backend and frontend on different servers



      app.delete('http://localhost:5000/api/surveys/delete/:surveyId', async (req, res) => 
      await Survey.deleteOne( _id: req.params.surveyId )

      const surveys = await Survey.find( _user: req.user.id ).select(
      recipients: false
      )
      res.send(surveys)
      )


      Off course you have to change the address once you deploy it to production and enable CORS




    2. Build the client app for producion, this process will compile all css, js and html and save it to client/build folder as stated on line



      res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));


      This will cause the frontend and backend code to run on the same server







    share|improve this answer



























    • Thank you very much for your answer. It made me realize that there might be a problem with the proxy.

      – Kenta. K
      Mar 29 at 4:12















    0
















    So if I undestood correctly, you have a Expressjs App and a client app running on different ports, if so I think I found the issue



    You are making ajax calls as if you had both frontend and backend running on the same server. Here are two possible solutions




    1. Change the address of the Axios delete action to point to the right server if you are planning to run the backend and frontend on different servers



      app.delete('http://localhost:5000/api/surveys/delete/:surveyId', async (req, res) => 
      await Survey.deleteOne( _id: req.params.surveyId )

      const surveys = await Survey.find( _user: req.user.id ).select(
      recipients: false
      )
      res.send(surveys)
      )


      Off course you have to change the address once you deploy it to production and enable CORS




    2. Build the client app for producion, this process will compile all css, js and html and save it to client/build folder as stated on line



      res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));


      This will cause the frontend and backend code to run on the same server







    share|improve this answer



























    • Thank you very much for your answer. It made me realize that there might be a problem with the proxy.

      – Kenta. K
      Mar 29 at 4:12













    0














    0










    0









    So if I undestood correctly, you have a Expressjs App and a client app running on different ports, if so I think I found the issue



    You are making ajax calls as if you had both frontend and backend running on the same server. Here are two possible solutions




    1. Change the address of the Axios delete action to point to the right server if you are planning to run the backend and frontend on different servers



      app.delete('http://localhost:5000/api/surveys/delete/:surveyId', async (req, res) => 
      await Survey.deleteOne( _id: req.params.surveyId )

      const surveys = await Survey.find( _user: req.user.id ).select(
      recipients: false
      )
      res.send(surveys)
      )


      Off course you have to change the address once you deploy it to production and enable CORS




    2. Build the client app for producion, this process will compile all css, js and html and save it to client/build folder as stated on line



      res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));


      This will cause the frontend and backend code to run on the same server







    share|improve this answer















    So if I undestood correctly, you have a Expressjs App and a client app running on different ports, if so I think I found the issue



    You are making ajax calls as if you had both frontend and backend running on the same server. Here are two possible solutions




    1. Change the address of the Axios delete action to point to the right server if you are planning to run the backend and frontend on different servers



      app.delete('http://localhost:5000/api/surveys/delete/:surveyId', async (req, res) => 
      await Survey.deleteOne( _id: req.params.surveyId )

      const surveys = await Survey.find( _user: req.user.id ).select(
      recipients: false
      )
      res.send(surveys)
      )


      Off course you have to change the address once you deploy it to production and enable CORS




    2. Build the client app for producion, this process will compile all css, js and html and save it to client/build folder as stated on line



      res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));


      This will cause the frontend and backend code to run on the same server








    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 28 at 19:48

























    answered Mar 28 at 19:39









    Erick CastrilloErick Castrillo

    3152 silver badges7 bronze badges




    3152 silver badges7 bronze badges















    • Thank you very much for your answer. It made me realize that there might be a problem with the proxy.

      – Kenta. K
      Mar 29 at 4:12

















    • Thank you very much for your answer. It made me realize that there might be a problem with the proxy.

      – Kenta. K
      Mar 29 at 4:12
















    Thank you very much for your answer. It made me realize that there might be a problem with the proxy.

    – Kenta. K
    Mar 29 at 4:12





    Thank you very much for your answer. It made me realize that there might be a problem with the proxy.

    – Kenta. K
    Mar 29 at 4:12













    0
















    Thank you very much for all answers and comments. The problem was the proxy. I am using http-proxy-middleware to handle requests between different ports in the dev environment. All comments about ports made me realize that I need to check the 'setupProxy.js' file. I modified the code inside the file as below and that fixed the problem.



    setupProxy.js



    const proxy = require('http-proxy-middleware')

    module.exports = function(app)
    app.use(proxy ('/auth/google', target: 'http://localhost:5000' ))
    app.use(proxy ('/api/**', target: 'http://localhost:5000' ))



    Thank you very much again for all the answers and comments.






    share|improve this answer





























      0
















      Thank you very much for all answers and comments. The problem was the proxy. I am using http-proxy-middleware to handle requests between different ports in the dev environment. All comments about ports made me realize that I need to check the 'setupProxy.js' file. I modified the code inside the file as below and that fixed the problem.



      setupProxy.js



      const proxy = require('http-proxy-middleware')

      module.exports = function(app)
      app.use(proxy ('/auth/google', target: 'http://localhost:5000' ))
      app.use(proxy ('/api/**', target: 'http://localhost:5000' ))



      Thank you very much again for all the answers and comments.






      share|improve this answer



























        0














        0










        0









        Thank you very much for all answers and comments. The problem was the proxy. I am using http-proxy-middleware to handle requests between different ports in the dev environment. All comments about ports made me realize that I need to check the 'setupProxy.js' file. I modified the code inside the file as below and that fixed the problem.



        setupProxy.js



        const proxy = require('http-proxy-middleware')

        module.exports = function(app)
        app.use(proxy ('/auth/google', target: 'http://localhost:5000' ))
        app.use(proxy ('/api/**', target: 'http://localhost:5000' ))



        Thank you very much again for all the answers and comments.






        share|improve this answer













        Thank you very much for all answers and comments. The problem was the proxy. I am using http-proxy-middleware to handle requests between different ports in the dev environment. All comments about ports made me realize that I need to check the 'setupProxy.js' file. I modified the code inside the file as below and that fixed the problem.



        setupProxy.js



        const proxy = require('http-proxy-middleware')

        module.exports = function(app)
        app.use(proxy ('/auth/google', target: 'http://localhost:5000' ))
        app.use(proxy ('/api/**', target: 'http://localhost:5000' ))



        Thank you very much again for all the answers and comments.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 29 at 4:26









        Kenta. KKenta. K

        361 silver badge7 bronze badges




        361 silver badge7 bronze badges
























            -2
















            CORS (Delete verb) must be enabled in your route middleware.



            This thread may help you.






            share|improve this answer

























            • This could be the case but Axios is not calling the right address

              – Erick Castrillo
              Mar 28 at 19:44











            • Thank you for your answer. The problem was the proxy. It didn't handle the request properly.

              – Kenta. K
              Mar 29 at 4:14















            -2
















            CORS (Delete verb) must be enabled in your route middleware.



            This thread may help you.






            share|improve this answer

























            • This could be the case but Axios is not calling the right address

              – Erick Castrillo
              Mar 28 at 19:44











            • Thank you for your answer. The problem was the proxy. It didn't handle the request properly.

              – Kenta. K
              Mar 29 at 4:14













            -2














            -2










            -2









            CORS (Delete verb) must be enabled in your route middleware.



            This thread may help you.






            share|improve this answer













            CORS (Delete verb) must be enabled in your route middleware.



            This thread may help you.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 28 at 18:33









            R. ZuiniR. Zuini

            12 bronze badges




            12 bronze badges















            • This could be the case but Axios is not calling the right address

              – Erick Castrillo
              Mar 28 at 19:44











            • Thank you for your answer. The problem was the proxy. It didn't handle the request properly.

              – Kenta. K
              Mar 29 at 4:14

















            • This could be the case but Axios is not calling the right address

              – Erick Castrillo
              Mar 28 at 19:44











            • Thank you for your answer. The problem was the proxy. It didn't handle the request properly.

              – Kenta. K
              Mar 29 at 4:14
















            This could be the case but Axios is not calling the right address

            – Erick Castrillo
            Mar 28 at 19:44





            This could be the case but Axios is not calling the right address

            – Erick Castrillo
            Mar 28 at 19:44













            Thank you for your answer. The problem was the proxy. It didn't handle the request properly.

            – Kenta. K
            Mar 29 at 4:14





            Thank you for your answer. The problem was the proxy. It didn't handle the request properly.

            – Kenta. K
            Mar 29 at 4:14


















            draft saved

            draft discarded















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55404279%2fgetting-404-not-found-on-axios-delete%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript