How to send / receive data from the client to the server, making requests like dynamically fetching the database?
How to send / receive data from the client to the server, making requests like dynamically fetching the database?
If you are using Express you can receive data via app.get () or app.post ()
Data passed in GET (url for example: google.com?foo=bar&animal=gato
) will be available via req.query
. An example would be:
app.get('/pasta', function(req, res){
var foo = req.query.foo;
// etc
});
Data passed in POST is available via req.body
. That is, if you have a <form action="/pasta" ...
and an input: <input type="text" name="animal" />
this data can be used like this:
app.post('/pasta', function(req, res){
var animal= req.body.animal;
// etc
});