0

I have configure app.js to redirect all request to my controller.js. The following is an example of how my controller.js looks like.

router.all('/Controller/:id', controller);
function controller(req, res){
    //db check using the id mentioned in the url. And gets some values.
    //Say it is stored in data variable.
    var URL = "SOMEURL";
    if(data != null){
        if(data.METHOD == "POST"){
            // needs to redirect as HTTP POST with URL.
        }else{
            res.redirect(URL);
        }
    }else{
        console.log('No Data Found to redirect');
    }
}

You can see that i need to redirect to a specific URL if the data.METHOD is POST. I tried res.redirect, but all it gives is a GET request. So How can i redirect as HTTP POST to a specific url? Please provide me a your ideas.

thanks and regards.

Demolition
  • 138
  • 1
  • 1
  • 10
  • from the top of my head, I would say that you need to create a new post request on that method, and return the response to the caller, maybe there are other ways.. or maybe I'm wrong – rmjoia Oct 04 '17 at 14:14
  • Why redirect? You have the data, you already know what you need to do. Why not just break the code you want to execute into a function on your server where it can be used by both your other route and called right here? It seems like you're doing a pointless redirect. – jfriend00 Oct 04 '17 at 14:34

2 Answers2

0

It would be better if you set up individual methods for each condition.

One for a GET, one for a POST,PUT etc. This way you can have testable code and not break the Single Responsibility Principle

router.get('/Controller/:id', controller.GetData);
router.put('/Controller/', controller.UpdateData);
router.post('/Controller/', controller.SaveData);

for put and post you can pass the data in the request.body

Rahul Ganguly
  • 1,908
  • 5
  • 24
  • 36
0

Thanks guys..

found my answer.

all i need to do is redirect with 307 HTTP status. The following will redirect to the post Router.

res.redirect(307, URL);
Demolition
  • 138
  • 1
  • 1
  • 10