Express GET and POST variable

GET params

app.get('/:name', function(req, res, next){
   var name = req.params.name;
   var getvar = req.query.getvar1;
});

TIPS: iterate over req.query to get all GET variables

POST params

app.use(express.bodyParser());
function errorHandler(err, req, res, next){
   console.error(err.message);
   console.error(err.stack);
   res.status(500);
   res.render('error_template', {error: err});
}
app.use(errorHandler);

app.post('/', function(req, res, next){
    var favourite = req.body.fruit;
    if(typeof favourite == 'undefined'){
         next(Error('Please choose a fruit!'));
    } else {
         // success 
    }
});
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s