The response is showing as buffer in the node server

2.1k Views Asked by At

I have created a simple react app. The problem which I'm finding is when I'm hitting the url, in the response, it is showing as buffer, which should not be the case.

My code

index.js

import axios from 'axios';

export const ADD_CART = "ADD_CART";
export const REMOVE_CART = "REMOVE_CART";
export const LOGIN = "LOGIN";


export const BASE_API_URL = "http://localhost:3030";

export function addToCart(item) {

    console.log(window.localStorage.getItem('WCToken'))
    console.log(window.localStorage.getItem('WCTrustedToken'))
    var headers = {
        'Content-Type': 'application/json',
        'WCToken': window.localStorage.getItem('WCToken'),
        'WCTrustedToken': window.localStorage.getItem('WCTrustedToken')
    }
    axios.post(BASE_API_URL + "/cart", {
        orderItem: [
            {
                productId: item.uniqueID, //working for 12262
                quantity: '1'
            }
        ]
    }, {headers: headers}).then(res => console.log(res))
        .catch(err => console.log(err));
    return {
        type: ADD_CART,
        payload: item
    };
}

export function removeFromCart(cartList, id) {
    return {
        type: REMOVE_CART,
        payload: cartList.filter(i => i.uniqueID != id)
    };
}

export const login = () => {
    return axios.post(BASE_API_URL + "/guestidentity", {}).then(res => {
        window.localStorage.setItem("WCToken", res.data.express.WCToken)
        window.localStorage.setItem("WCTrustedToken", res.data.express.WCTrustedToken)
        return {
            type: LOGIN,
            payload: {}
        }
    }).catch(e => {
        console.log(e);
        return {
            type: LOGIN,
            payload: {}
        }
    });
};

server.js

const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const Client = require('node-rest-client').Client;//import it here
const app = express();
const helmet = require('helmet');
const morgan = require('morgan');

// enhance your app security with Helmet
app.use(helmet());

// use bodyParser to parse application/json content-type
app.use(bodyParser.json());

// log HTTP requests
app.use(morgan('combined'));

app.use(cors());

process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0

app.post('/guestidentity',(req, res) => {
    console.log("Hello from Server") 
    var client = new Client();

    // direct way
    client.post("https://149.129.128.3:5443/wcs/resources/store/1/guestidentity", (data, response) => {
        res.send({ express: data });
    });
});



app.post('/cart',(req, res) => {
    console.log("Hello from Server") 
    var client = new Client();

    // direct way
    client.post("https://149.129.128.3:5443/wcs/resources/store/1/cart", (data, response) => {
        res.send({ express: data });
    });
});

const port = 3030;
app.listen(port, () => console.log(`Server running on port ${port}`));

enter image description here

I dont know where my code is getting wrong and why buffer is showing in response. Can somebody please guide me on this. Or provide me an insight how to troubleshoot this issue.

1

There are 1 best solutions below

3
On

You are sending to the client an object with the form:

{
    express: data
}

And when you log that on the console you see that data is an object:

{
    data: [],
    type: "buffer"
}

Which means that your post request inside express is returning that object.