Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #14: Middleware hangs if rawBody is already parsed #19

Merged
merged 3 commits into from
Feb 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,33 @@ alexaRouter.get('/weather_info', function(req, res) { ... })
app.listen(3000)
```

### Common errors
* `The raw request body has already been parsed.`- This means that you're probably using one of the body-parser middlewares and it is loaded before this one. To fix it, you should load the body-parsers **after** this one.

Before:
```javascript
var alexaRouter = express.Router()
app.use('/alexa', alexaRouter)

// INCORRECT
alexaRouter.use(bodyParser.json());
alexaRouter.use(verifier)
```

After:
```javascript
var alexaRouter = express.Router()
app.use('/alexa', alexaRouter)

// CORRECT
alexaRouter.use(verifier)
alexaRouter.use(bodyParser.json());
```

### Mentions
* [mreinstein](https://github.com/mreinstein) for his [alexa-verifier](https://github.com/alexa-js/alexa-verifier) module, which allows you to verify any Amazon requests from any web service

### License
Copyright (c) 2016-2017 Tejas Shah

MIT License, see [LICENSE](https://tejashah88.mit-license.org/2016-2017) for details.
MIT License, see [LICENSE](https://tejashah88.mit-license.org/2016-2017) for details.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ var verifier = require('alexa-verifier')
// text string, and set a flag on the request object so other body parser
// middlewares don't try to parse the body again
module.exports = function alexaVerifierMiddleware(req, res, next) {
if (req._body && (typeof req.rawBody !== 'string')) {
var er = 'The raw request body is not available.'
if (req._body) {
var er = 'The raw request body has already been parsed.'
return res.status(400).json({ status: 'failure', reason: er })
}

Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ test('fail if request body is already parsed', function(t) {

t.equal(mockRes.statusCode, 400);
t.deepEqual(JSON.parse(mockRes._getData()), {
reason: 'The raw request body is not available.',
reason: 'The raw request body has already been parsed.',
status: 'failure'
});

Expand Down