javascript - Node JS: Listening for HTTPS POST JSON Requests -


i have network server posting information me json payloads specific url endpoint.

the specification states need standup https server can listen post json requests.

i've created simple node js listener shown below.

i can confirm request hitting endpoint, when logged, appears garbled data. can please tell me if i'm missing parameter in script preventing json decoding happening correctly?

var express = require('express'); var http = require('http'); var https = require('https'); var fs = require('fs'); var logger = require('logger'); var bodyparsernew = require('body-parser'); var application_root = __dirname; var path = require("path"); var util = require('util');  var options = {   key: fs.readfilesync('./privatekey.pem'),   cert: fs.readfilesync('./server.crt') };  var app = express();  app.set('port', process.env.port); app.use(express.static(path.join(__dirname, 'site')));  //listener app.post('/api/test', function (req, res) {     var body = '';     req.on('data', function (data) {         body += data;     });     req.on('end', function () {         console.log('/api/test');         console.log(req.body);     });       res.writehead(200);     res.end(); });  https.createserver(options,app).listen(8080); console.log('listening on server 8080'); 

snippet of payload i'm seeing:

��qk�0��r��ْ,k򽅦��1$e!y�Ä��2�����b��n)�e�7��:�ü�f7v���- �?�3���g��� �                  9���=#�4��hhv���k��y�0�%Øq&�mÄ7�i�a�c>��a???h,�����1�5�����2Äe�<�?�����q�� 

try moving res.writehead(200); res.end(); inside req.on('end') callback, otherwise you're sending response before waiting request data.

also, don't need buffering request data manually. adding app.use(bodyparsernew.json()); above route , provide req.body containing parsed result. can set bodyparser.json() options if need them.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -