Skip to content
Advertisement

How to redirect from a mysql query in node.js webapp?

I’m making a simple node.js webapp for inserting data into a database with a condition – when there’s two instances with the same time attribute, the insert query isn’t called and you get redirected to a simple html page with a message that you have to pick another time.

My question is, how can I redirect from a block of code belonging to query? or is there another way to let user know that he has to pick another time?

JavaScript

Advertisement

Answer

You can call resp.redirect('/fail'); inside your query block, or anywhere in function(req, resp) function. You don’t need to throw and catch.

As another suggestion: you can try parameter embedding for your sqls. otherwise you can be subject to sql injection attacks.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement