Skip to content

Instantly share code, notes, and snippets.

@shurane
Last active December 13, 2015 23:48
Show Gist options
  • Save shurane/4993778 to your computer and use it in GitHub Desktop.
Save shurane/4993778 to your computer and use it in GitHub Desktop.
I'm running an express.js app, and req.params, req.body, and req.query are empty! Even though I'm trying to send JSON data over POST to it! Order matters. Move all the app.use() calls before app.post and voila. Works.
var express = require('express');
var app = express();
// routes
app.post('/payment', function(req, res){
console.log("======");
console.log(req.headers);
console.log(req.params);
console.log(req.body);
console.log(req.query);
res.send('Hello World\n');
});
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.logger());
app.listen(3000);
// console.log time
//req.headers
{ 'user-agent': 'curl/7.28.1',
host: 'localhost:3000',
accept: 'application/json',
'content-length': '37',
'content-type': 'application/x-www-form-urlencoded' }
//req.params
{ path: '/payment',
method: 'post',
callbacks: [ [Function] ],
keys: [],
regexp: /^\/payment\/?$/i,
params: [] }
$ npm version
{ http_parser: '1.0',
node: '0.8.15',
v8: '3.11.10.25',
ares: '1.7.5-DEV',
uv: '0.8',
zlib: '1.2.3',
openssl: '1.0.1c',
npm: '1.1.66',
}
$ npm list
├─┬ express@3.1.0
│ ├── buffer-crc32@0.1.1
│ ├── commander@0.6.1
│ ├─┬ connect@2.7.2
│ │ ├── bytes@0.1.0
│ │ ├── formidable@1.0.11
│ │ ├── pause@0.0.1
│ │ └── qs@0.5.1
│ ├── cookie@0.0.5
│ ├── cookie-signature@0.0.1
│ ├── debug@0.7.2
│ ├── fresh@0.1.0
│ ├── methods@0.0.1
│ ├── mkdirp@0.3.3
│ ├── range-parser@0.0.4
│ └─┬ send@0.1.0
│ └── mime@1.2.6
├── stripe@1.3.0
$ curl -i -H "Accept: application/json" -X POST -d '{"bedrooms":"4", "squarefeet":"1250"}' http://localhost:3000/payment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment