0

I want to load 'get-all' page when I am in a main page. The code is below:

const express = require('express');
const app = express();

const PORT = process.env.PORT || 3000;

app.listen(PORT, () => { console.log(`express server is listening on port ${PORT}`) });

app.get('/', (req: any, res: any) => {
    res.redirect('/get-all');
});

app.post('/create', (req: any, res: any) => {

});

app.post('/get-all', (req: any, res: any) => {
    res.send('get all') // error: get all not found
});

app.post('/delete', (req: any, res: any) => {

});

app.post('/update', (req: any, res: any) => {

});

The error is: Cannot GET /get-all (I tried to run it via Postman). I didn't find clear answer in the documentation. Thanks!

1 Answers1

4

No. Redirects can switch a request to GET but not to POST.


It doesn't make much sense to do that anyway. GET requests are supposed to be Safe and Idempotent. Switching a POST request (which aren't) is a fairly dangerous form of unexpected behaviour. The request wouldn't have a body containing POST data anyway.

Your method is called "get-all" and doesn't seem to be storing/updating/changing anything anyway, so the proper verb to access it would be GET in the first place.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335