0

I have an express web app that has app.use(express.json()). I'm testing the integrations with Slack's slash commands, and I had a very basic post function set up:

app.post('/info', (req, res) => {
    console.log(req.body);
    res.send('Thanks');
});

The issue is whenever my slack app hits /info, my app throws

TypeError: Converting circular structure to JSON
    at stringify (/node_modules/express/lib/response.js:1119:12)
    at ServerResponse.json (/node_modules/express/lib/response.js:260:14)
    at app.post (app.js:69:9)

Is there a way to fix this error without removing the app.use(express.json())?

I've seen this answer, however I don't believe it pertains as I can't modify express' express.json() function. I've also seen this answer, however it seems like that's due to their use of res.json which I am not using.

Beaurocks16
  • 341
  • 1
  • 7
  • 16
  • Are you looking for a solution to catch the error? I.e. preventing your server from crashing if a circular json is sent? – eol Apr 13 '19 at 10:05
  • @eol Well, I'm wondering why this happens and how to make it not happen. Currently, I'm unable to ever handle `/info` post request because of it, which is definitely not desirable. – Beaurocks16 Apr 13 '19 at 20:27
  • Are you sending a simple text as the response to the slash command, just like you posted here, or sending JSON? If you're sending JSON, how are you sending? I am suspecting the JSON structure contains a circular reference. Alternatively, just use `res.json(obj)` to send a JSON, instead of using the middleware. – girlie_mac Apr 22 '19 at 20:05

1 Answers1

0

I haven't seen your POST payload so I cannot tell for sure. However, it is possible that you are running into this scenario because you from Slack you are POST'ing with the Header {Content-Type: application/json} already and inside your Express app you are using the middleware express.json() again. If you already know that clients must send the Content-Type: application/json and you expect json object why do you have to use the express.json()?

  • If you don't understand the question, please add a comment instead of answering it. – troy Apr 03 '20 at 07:30
  • @troy - Thanks for your feedback. Since I am new to SO, I am happy to take your feedback. A couple of things: 1. I am not sure what makes you think I didn't understand the question. Please clarify it. ( I happened to have ran into similar problem, found a solution and wanted to help ) 2. If the OP comes back saying what I have suggested solved the problem what happens? 3. It also looks like I need "50 reputation" to comment on the OP's post. 4. Last but not least, do you have a solution/answer to help OP? – Amarbayar Amarsanaa Apr 05 '20 at 09:40