3

I am getting 405 Method Not Found as response when using PUT and DELETE request using POSTMAN. My app.js file looks like this

 const Koa = require('koa');
 const serve = require('koa-static');
 const cors = require('@koa/cors');
 const userRouter = require('./routes/users');
 const port = process.env.PORT || 3000;
 var app = new Koa();
 app.use(serve(__dirname + '/view'));
 app.use(cors());
 app.use(userRouter.routes());
 app.use(userRouter.allowedMethods());
 app.listen(port, () => {
     console.log(port);
 });

and my ./routes/users file looks something like this

     router.get('/users/:id', getData)
              .post('/users', postData)
              .put('/users/:id', updateData)
              .del('/users/:id', deleteData);

Also when I check response headers section in POSTMAN it shows

  Allow →HEAD, GET
  Connection →keep-alive
  Content-Length →18
  Content-Type →text/plain; charset=utf-8
  Date →Thu, 15 Feb 2018 05:25:36 GMT
  Vary →Origin

I've also tried all the possible solutions out there on internet and git issues but still no use Please help me fix this.

  • Hmm, would need to see more of your code. Could you share your main `app.js` file? – Saad Feb 17 '18 at 21:49
  • @saadq Here is the `app.js` file `const Koa = require('koa');const serve = require('koa-static');const userRouter = require('./routes/users');const port = 3000;var app = new Koa();app.use(serve(__dirname + '/view')); app.use(userRouter.routes());app.use(userRouter.allowedMethods();app.listen(port, () => {console.log(port);});` Here is the `./routes/users` file `const router = require('koa-router')();const body = require('koa-body');router.get('/users/:id', getData).post('/users', insertData).put('/users/:id', updateData).del('/users/:id', deleteData);` – Shrinath Prabhu Feb 19 '18 at 03:48
  • @saadq Please bear with this, I had to remove all spacings because comments had a limit of characters but these are the files – Shrinath Prabhu Feb 19 '18 at 03:50
  • **.put('/users/:id', updateData)** method shouldn't have **/:id** it is same as post method accepts body. – sultan Mar 07 '18 at 17:55
  • @sultan we can use it – Shrinath Prabhu Mar 08 '18 at 03:53
  • @ShrinathPrabhu how you solve the problem? I am getting the same while calling the PUT request – chetan mekha Jun 25 '21 at 18:44
  • @chetanmekha Have u solve it? I met this today. – JackChouMine Oct 23 '21 at 08:20
  • @JackChouMine Yes, we resolved it with help of setting proxy and working good. – chetan mekha Oct 25 '21 at 08:44

0 Answers0