Skip to content

Instantly share code, notes, and snippets.

@eiriklv
Last active October 19, 2015 15:55
Show Gist options
  • Save eiriklv/ba3379cf0355217d082d to your computer and use it in GitHub Desktop.
Save eiriklv/ba3379cf0355217d082d to your computer and use it in GitHub Desktop.
Simple express example with body parsing
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var tokenChallenge = 'XXX';
/**
* Add automatic body parsing,
* so that you have req.body
* available inside the route handler
*/
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
/**
* Respond to a GET request to /
*/
app.get('/', function(req, res) {
res.send('Hello world');
});
/**
* Respond to a POST request to /
*/
app.post('/', function(req, res) {
/**
* Inside here you have the following (of interest) available
* - req.query (already parsed)
* - req.body (already parsed)
*
* This is provided by express behind
* the scenes (check out the express docs for more info)
*/
if (req.body.token !== tokenChallenge) {
res.send('You shall not pass!')
} else {
res.send('You may enter');
}
});
app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment