Home > Software engineering >  Getting 404 CANNOT POST and CANNOT PUT errors in Postman
Getting 404 CANNOT POST and CANNOT PUT errors in Postman

Time:07-21

I'm making an API and I implemented the POST and PUT requests. Whenever I test my API on Postman, I get 404 errors that say "CANNOT POST /index/" and "CANNOT PUT /index/". I've looked through tons of similar posts like this but I can't find a solution that works. I'm using node as well express to set up routes.

Here is my app.js code:

const express = require('express');
const app = express();
const rateLimit = require('express-rate-limit');
const cors = require('cors');
const bodyParser = require('body-parser');

const indexRouter = require('./routes/documentOperations');


app.use(cors({
   methods: ["GET", "POST", "PUT"],
   credentials: true,
   origin: true
}));

app.use(express.static(__dirname   '/public'));

app.use(express.json());

app.use(bodyParser.json());

app.use(rateLimit({
   windowMs: 10 * 60 * 1000,
   max: 200}));

app.use('/index', indexRouter);

app.listen(process.env.PORT, (err) => {
   if (err) console.log(err);
   console.log("API server listening on Port: ", process.env.PORT);});

module.exports = {client, app};

I think this may be a routing problem, although I'm sure that my routing is correct. My router for '/index' only has '/:indexId' paths (so I basically did something like router.post('/:indexId', function())) but with different API methods, so to test the API, I just test it with http://localhost:7700/index/?indexId=value. And I've imported my router to app.js correctly. So, I'm not sure what the issue is. Any help is appreciated!

EDIT:

Here is a screenshot of a PUT Request: put request

Here is a screenshot of the resulting Error: Error when I make the corresponding PUT request

CodePudding user response:

The error message you give says /index/ and your code says /index, could this be it? Otherwise can you take a screenshot of the whole error and your request?

Change app.use('/index', indexRouter); to app.use('/index/', indexRouter); and then you can use req.query.indexId to read the value of the indexId parameter.

OR

Use the url http://localhost:7700/index?indexId=test1 and the same as the first option, you can use req.query.indexId to read the value of the indexId parameter.

  • Related