I am using new browser feature(navigator.sendBeacon) to POST async data to node.js server.
But i am unable to receive it on node server. So could any one tell me how to receive data posted by sendBeacon on node server.
node server code is:
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
// set cross origin header to allow cross-origin request.
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(bodyParser.json());
app.post('/',function(req,res){
console.log('i got the request',req.body)
});
var server = app.listen(3000, function() {
console.log('Express is listening to http://localhost:3000');
});
client side code
navigator.sendBeacon('http://localhost:3000/','{"a":9}')