In node, I'm using express.router() to receive a curl request. I just want to do a "hello world" and log whatever I've received
My curl request:
curl -F "hello_world=foobar" http://examplesite.com/my_endpoint
How Node handles it:
var express = require('express');
var router = express.Router();
router.route('/my_endpoint')
.post(function(req, res){
var data = req.body;
console.log(data); // This comes back blank... shouldn't it have "hello_world=foobar"?
});
I would do it a little differently,
var express = require('express');
var app = express();
var http = require('http').Server(app);
app.post('/myendpoint', function (req, res) {
var body = "";
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
console.log(body); //will print hello_world=Foobar
});
req.body doesnt exist naturally within a Express-App. For that, you need express´s body-parser middleware. Install it with:
npm install body-parser --save
After that, you can easily add it to your app by like this:
var express = require('express');
var bodyParser = require('body-parser');
var app = express(); // Instantiate an express app
app .use(bodyParser.urlencoded({ extended: false })); // parse application/x-www-form-urlencoded
app.use(bodyParser.json()); // parse application/json
var router = app.Router();
router.route('/my_endpoint')
.post(function(req, res){
var data = req.body;
console.log(data); // This comes back blank... shouldn't it have "hello_world=foobar"?
});
EDIT I also saw you have a little mistake by how you are instantiating your express app. You dont use the router on express itself, but on an app you instantiate before by just calling express. I edited it in my above example.