Last active
October 19, 2015 15:55
-
-
Save eiriklv/ba3379cf0355217d082d to your computer and use it in GitHub Desktop.
Simple express example with body parsing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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