响应在reactjs应用中显示为缓冲区

时间:2018-11-06 07:00:16

标签: node.js reactjs express

我创建了一个简单的React应用。我发现的问题是当我击中url时,响应中它显示为缓冲区,而不是这种情况。

我的代码

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

我不知道我的代码在哪里出错以及为什么缓冲区显示为响应。有人可以在这方面指导我吗。或为我提供有关如何解决此问题的见解。

1 个答案:

答案 0 :(得分:0)

您可能需要配置客户端以将响应解析为JSON。

var options = {
    mimetypes: {
        json: ["application/json", "application/my-custom-content-type-for-json;charset=utf-8"]

    }
};

var client = new Client(options);