Saturday, 16 February 2019

How will I make sure I receive a valid JSON? (using express and getting them as POST messages)

I never programmed for web stuff, and I just started learning nodejs. I need to write a simple (simplified example) server (for work) that listens to POST messages with json data, and save those JSONs. I didn't get 100% of the details yet, so I'm not sure in what format I'm going to receive these JSONs. I know it's going to be POST messages with JSON data.​So currently I'm using express with express.json()var app = express(); app.use(express.json()); but the problem with express.json() is that it only takes care of post messages with application/json as headers. And how I should know that's how I'd get them?​In addition, if the body of the JSON is bad, then the program gets an exception at express.json().​Anyway, I'm not sure if using express.json() is the right call. Isn't it just better to get ANY post message, and just check the string format if it's JSON with JSON.parse? But this solution may also be problematic because I need my program to scale.​In conclusion (or TLDR), The sole purpose of this program is to receive POST messages with JSONs in them. currently I'm using express.json but not sure if this was the right call. What do you think I should do?​

Submitted February 16, 2019 at 12:59PM by sheldonzy

No comments:

Post a Comment