dace
dace

Reputation: 6363

How to send a POST request response back to client (React component)?

I'm using React to send a post request to my express server which then returns a response, like so:

app.post('/api/games', function(req, res) {
   return res.json();
});

Before I use that response to persist it to a database, I want to be able to see everything within that response. When I console.log it from the server, I get a long list of the JSON object printed, but would ideally like something closer to Chrome's dev tools console where I can click through and inspect everything. How can I send this response JSON back to my React component so that I can console.log it in in the browser?

Thanks!

Upvotes: 1

Views: 4106

Answers (3)

G Sis
G Sis

Reputation: 199

If you want to view your response in the client-tier here are some of the things you can do using the GoogleChrome asides from the console

1 Use React Developer Tools

2 I like using Postman for viewing and manipulating request/response

Upvotes: 0

Blair Anderson
Blair Anderson

Reputation: 20161

Remember, a request comes in, and the response goes out.

on a request, the req.body and req.params will contain the body/params of the request.

To respond to a request, use the res.json({someJSONHere}) or you can also res.send({someJson})

You can also console.log inside the server and watch output on the terminal

app.post('/api/games', function(req, res) { 
 console.log(req.params)
 console.log(req.body)
 return res.json(
   Object.assign({}, req.body, req.params)
 );
});

Express request/response Documentation

Upvotes: 0

QoP
QoP

Reputation: 28397

You can use node-inspector in order to debug your express API.

It uses Chrome Dev Tools so you'll feel like you are debugging your client side!

Upvotes: 1

Related Questions