How to handle GET and POST request in Node.js Express
Handle GET Request
HTML GET form:
<form method="get" action="/"> <input type="text" name="username"> <input type="submit"> </form>
Handler:
app.get('/', function(req, res) { res.send('Username: ' + req.query['username']); });
A good way to remember
req.query is that the GET data is stored in the URL query string - querycomes from the query string.
Handle POST Request
HTML POST form:
<form method="post" action="/"> <input type="text" name="username"> <input type="submit"> </form>
Handler code:
app.post('/', function(req, res) { res.send('Username: ' + req.body.username); };
There it is! Easy GET and POST request handling in Node.js Express framework.


Comments
Post a Comment