I'm trying to make a very basic node.js app using express, that takes browser event logs from log4javascript over CORS and HTTPS and that will write them to mongoDB. I'm stuck at trying to get the bodyParser to show the data. I've tried all sorts of bodyParser configurations, but req.body, req.params and req.query are always empty.
Here is my code.
var fs = require('fs'),
bodyParser = require('body-parser'),
express = require('express'),
cors = require('cors'),
app = express(),
https = require('https'),
key = fs.readFileSync('*****/key.pem'),
cert = fs.readFileSync('*****/cert.pem'),
https_options = {
key: key,
cert: cert
};
app.use(bodyParser.json());
app.use(cors());
app.post('/', function (req, res) {
console.log(req);
res.send('Hello World!')
});
https.createServer(https_options, app).listen(3443);
I am using a JSON Layout in log4javascript. Here is my frontend code:
var logger = log4javascript.getDefaultLogger(),
userAgent = navigator.userAgent.toLowerCase(),
ajaxAppender = new log4javascript.AjaxAppender("https://***/log/");
ajaxAppender.addHeader("Content-Type", "application/json");
jsonLayout = new log4javascript.JsonLayout(false, false);
ajaxAppender.setLayout(jsonLayout);
ajaxAppender.setBatchSize(10);
ajaxAppender.setThreshold(log4javascript.Level.INFO);
logger.addAppender(ajaxAppender);
and here is a sample of what log4javascript is sending according to Chrome:
Request Payload [{"logger":"[default]","timestamp":1421974733969,"level":"INFO","url":"https://*****/","message":["{\"message\":\"User clicked to hide a form\",\"form\":\"audio\",\"userAgent\":\"mozilla/5.0 (macintosh; intel mac os x 10_10_1) applewebkit/537.36 (khtml, like gecko) chrome/39.0.2171.95 safari/537.36\",\"browserTimestamp\":\"Fri, 23 Jan 2015 00:58:53 GMT\",\"browserUnixTime\":1421974733968,lds_display_name=barnaby_b%40testing.****.com\"}"]},{"logger":"[default]","timestamp":1421974734498,"level":"INFO","url":"https://*******.com/","message":["{\"message\":\"User clicked to expand a form\",\"form\":\"audio\",\"userAgent\":\"mozilla/5.0 (macintosh; intel mac os x 10_10_1) applewebkit/537.36 (khtml, like gecko) chrome/39.0.2171.95 safari/537.36\",\"browserTimestamp\":\"Fri, 23 Jan 2015 00:58:54 GMT\",\"browserUnixTime\":1421974734497,lds_display_name=barnaby_b%40testing.*****.com\"}"]}, .... etc.
What am I doing wrong? Does CORS, or HTTPS, or APACHE mod_proxy, or anything else interfere with Express's ability to parse the body? Does Log4Javascript send the POST in some strange way that I would need to configure for the body to be readable? Please help!