Sunday, 12 March 2017

Deserialisation is not working properly for JSONAPI-Serialize Module in NodeJS

I am trying to perform serialization and de-serialization using JSONAPI-Serializer module of NodeJS.

Scenario that i wanted to make is user enter the values and value got serialized and then deserialize and show de-serialized output in response.

For this, I have coded a webserver using express module in NodeJS as shown below:-

var express = require('express');
var app = express();
var JSONAPISerializer = require('jsonapi-serializer').Serializer;
var JSONAPIDeserializer = require('jsonapi-serializer').Deserializer;


var port = process.env.PORT || 1337;

var bodyParser = require('body-parser');
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ extended: true })); 
app.post('/', function(req, res) {

    
    var data = [{"id":new Buffer(req.body.id, 'base64').toString(), "token":"test", "geo":"Test"}];

    var UserSerializer = new JSONAPISerializer('id', {attributes: ['id']});
    var users = UserSerializer.serialize(data);

    var UserDeserialize = new JSONAPIDeserializer();  
    var test = UserDeserialize.deserialize(users).then(a => console.log(a))
    res.json(test);
});
    
app.listen(port);
console.log('Server started! At http://localhost:' + port);

So, I am supplying base64 encoded user input and below are the request and responses for the same.

Request:

POST / HTTP/1.1
Host: localhost:1337
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:52.0) Gecko/20100101 Firefox/52.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: close
Upgrade-Insecure-Requests: 1
X-Forwarded-For: 127.0.0.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 11

id=dGVzdA==

Response:-

HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 40
ETag: W/"28-1v0wyAa9HM+8rbZDXQJnnGFWYjo"
Date: Sun, 12 Mar 2017 08:22:11 GMT
Connection: close

{"isFulfilled":false,"isRejected":false}

But I am expecting the response like this:-

[ { id: 'test' } ]

what I am doing wrong here ?



via Ashu

No comments:

Post a Comment